We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1541fd3 commit 45a9bd0Copy full SHA for 45a9bd0
solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java
@@ -1,10 +1,20 @@
1
class Solution {
2
+ private Integer[] f;
3
+ private int[] cost;
4
+
5
public int minCostClimbingStairs(int[] cost) {
- int n = cost.length;
- int[] f = new int[n + 1];
- for (int i = 2; i <= n; ++i) {
6
- f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
+ 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));
17
}
- return f[n];
18
+ return f[i];
19
-}
20
+}
0 commit comments