Skip to content

Commit

Permalink
243rd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 26, 2024
1 parent b67c99d commit 066a842
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,12 @@ Ace Coding Interview with 75 Qs
| DP - 1D | | |
| ------------------------------ | ---------------- | ------ |
| 1137. N-th Tribonacci Number | [Solution][1137] | Easy |
| 746. Min Cost Climbing Stairs | Solution | Easy |
| 746. Min Cost Climbing Stairs | [Solution][746] | Easy |
| 198. House Robber | Solution | Medium |
| 790. Domino and Tromino Tiling | Solution | Medium |

[1137]: ./src/page-11/1137.%20N-th%20Tribonacci%20Number/tribonacci.ts
[746]: ./src/page-7/746.%20Min%20Cost%20Climbing%20Stairs/minCostClimbingStairs.ts

| DP - Multidimensional | | |
| --------------------------------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { minCostClimbingStairs } from './minCostClimbingStairs';

describe('746. Min Cost Climbing Stairs', () => {
test('minCostClimbingStairs', () => {
expect(minCostClimbingStairs([10, 15, 20])).toBe(15);
expect(minCostClimbingStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1])).toBe(6);
});
});
16 changes: 16 additions & 0 deletions src/page-7/746. Min Cost Climbing Stairs/minCostClimbingStairs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type MinCostClimbingStairs = (cost: number[]) => number;

/**
* Accepted
*/
export const minCostClimbingStairs: MinCostClimbingStairs = (cost) => {
const n = cost.length;
const dp: number[] = new Array(n + 1).fill(0);

// Populate dp array using the state transition
for (let i = 2; i <= n; i++) {
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}

return dp[n];
};

0 comments on commit 066a842

Please sign in to comment.