Skip to content

Commit 1541fd3

Browse files
authored
Update Solution.go
1 parent b83575f commit 1541fd3

File tree

1 file changed

+15
-5
lines changed
  • solution/0700-0799/0746.Min Cost Climbing Stairs

1 file changed

+15
-5
lines changed
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
func minCostClimbingStairs(cost []int) int {
22
n := len(cost)
3-
f := make([]int, n+1)
4-
for i := 2; i <= n; i++ {
5-
f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2])
3+
f := make([]int, n)
4+
for i := range f {
5+
f[i] = -1
66
}
7-
return f[n]
8-
}
7+
var dfs func(int) int
8+
dfs = func(i int) int {
9+
if i >= n {
10+
return 0
11+
}
12+
if f[i] < 0 {
13+
f[i] = cost[i] + min(dfs(i+1), dfs(i+2))
14+
}
15+
return f[i]
16+
}
17+
return min(dfs(0), dfs(1))
18+
}

0 commit comments

Comments
 (0)