Skip to content

Commit 5e6deaf

Browse files
authored
Added LC 746 java solution (#141)
1 parent 2814009 commit 5e6deaf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
int[] cost;
3+
int[] memo;
4+
int n;
5+
6+
private int solve(int pos) {
7+
if (pos >= n) {
8+
return 0;
9+
} else if (memo[pos] != -1) {
10+
return memo[pos];
11+
} else {
12+
memo[pos] = cost[pos] + Math.min(solve(pos + 1), solve(pos + 2));
13+
14+
return memo[pos];
15+
}
16+
}
17+
18+
public int minCostClimbingStairs(int[] cost) {
19+
this.cost = cost;
20+
n = cost.length;
21+
memo = new int[n + 1];
22+
23+
for (int i = 0; i <= n; i++) {
24+
memo[i] = -1;
25+
}
26+
27+
return Math.min(solve(0), solve(1));
28+
}
29+
}

0 commit comments

Comments
 (0)