-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b67c99d
commit 066a842
Showing
3 changed files
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/page-7/746. Min Cost Climbing Stairs/minCostClimbingStairs.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/page-7/746. Min Cost Climbing Stairs/minCostClimbingStairs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |