-
Notifications
You must be signed in to change notification settings - Fork 0
/
0747-min-cost-climbing-stairs.java
65 lines (58 loc) · 1.91 KB
/
0747-min-cost-climbing-stairs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
class Solution {
public int minCostClimbingStairs(int[] cost) {
return topdown(cost);
// return bottomup(cost); // 1초
// Map 사용
// return topdown2(cost); // 3초
// return downup2(cost); // 4초
}
public static int downup2(int[] cost) {
int n = cost.length;
Map<Integer, Integer> memo = new HashMap<>();
memo.put(1,0);
memo.put(0,0);
for(int i =2; i <= n ; i++) {
int min = Math.min(memo.get(i-1) + cost[i-1],
memo.get(i-2) + cost[i-2]);
memo.put(i, min);
}
return memo.get(n);
}
public static int topdown2(int[] cost) {
int n = cost.length;
Map<Integer, Integer> memo = new HashMap<>();
return dp2(memo,cost, n);
}
public static int dp2(Map<Integer, Integer> memo,int[] cost, int n) {
if(n == 0 || n == 1) return 0;
if(!memo.containsKey(n)) {
memo.put(n,Math.min(dp2(memo, cost, n-1) + cost[n-1], dp2(memo,cost, n-2) + cost[n-2]));
}
return memo.get(n);
}
public static int bottomup(int[] cost) {
int n = cost.length;
int[] memo = new int[n+1];
memo[0] = 0;
memo[1] = 0;
for(int i =2; i <= n ;i++)
memo[i] = Math.min(memo[i-1]+cost[i-1] , memo[i-2]+cost[i-2]);
return memo[n];
}
public static int topdown(int[] cost) {
int n = cost.length;
int[] memo = new int[n+1];
Arrays.fill(memo,-1);
return dp(memo,cost, n);
}
public static int dp(int[] memo, int[] cost, int n) {
if(n == 0 || n == 1) return 0;
if(memo[n] == -1) {
memo[n] = Math.min(dp(memo, cost, n-1) + cost[n-1] , dp(memo, cost, n-2) + cost[n-2]);
}
return memo[n];
}
}
// f(n) = f(n-1) + f(n-2)
// f(0) = 0, f(1) = 0