Skip to content

Commit a5ef3b3

Browse files
committed
GO: 746. Min Cost Climbing Stairs
1 parent 7b13263 commit a5ef3b3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

go/746-Min-Cost-Climbing-Stairs.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func minCostClimbingStairs(cost []int) int {
2+
n := len(cost)
3+
dp := make([]int, n+1)
4+
5+
if n == 2 {
6+
return min(cost[0], cost[1])
7+
}
8+
9+
for i := 2; i <= n; i++ {
10+
a := dp[i-1] + cost[i-1]
11+
b := dp[i-2] + cost[i-2]
12+
13+
dp[i] = min(a, b)
14+
}
15+
16+
return dp[n]
17+
}
18+
19+
func min(x, y int) int {
20+
if x > y {
21+
return y
22+
}
23+
24+
return x
25+
}

0 commit comments

Comments
 (0)