Skip to content

Commit 45a9bd0

Browse files
authored
Update Solution.java
1 parent 1541fd3 commit 45a9bd0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
class Solution {
2+
private Integer[] f;
3+
private int[] cost;
4+
25
public int minCostClimbingStairs(int[] cost) {
3-
int n = cost.length;
4-
int[] f = new int[n + 1];
5-
for (int i = 2; i <= n; ++i) {
6-
f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
6+
this.cost = cost;
7+
f = new Integer[cost.length];
8+
return Math.min(dfs(0), dfs(1));
9+
}
10+
11+
private int dfs(int i) {
12+
if (i >= cost.length) {
13+
return 0;
14+
}
15+
if (f[i] == null) {
16+
f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2));
717
}
8-
return f[n];
18+
return f[i];
919
}
10-
}
20+
}

0 commit comments

Comments
 (0)