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 2814009 commit 5e6deafCopy full SHA for 5e6deaf
Algorithms/Easy/746_MinCostClimbingStairs/Solution.java
@@ -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
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