-
Notifications
You must be signed in to change notification settings - Fork 0
/
60.MinCostClimbingStairs.java
51 lines (46 loc) · 1.29 KB
/
60.MinCostClimbingStairs.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
// Tabulation + O(1) space
class Solution {
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int first = cost[0];
int second = cost[1];
if(n<=2) return Math.min(first,second);
for(int i = 2;i<n;i++){
int curr = cost[i] + Math.min(first,second);
first = second;
second = curr;
}
return Math.min(first,second);
}
}
/* Bottom up Tabulation
class Solution {
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int[] dp = new int[n+1];
for(int i =0;i<n;i++){
if(i<2) dp[i] = cost[i];
else dp[i] = cost[i] + Math.min(dp[i-1],dp[i-2]);
}
return Math.min(dp[n-1],dp[n-2]);
}
}
*/
/* TLE - Memoisation
class Solution {
// Top Down Memoization - O(n) 1ms
int[] dp;
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
dp = new int[n];
return Math.min(minCost(cost, n-1), minCost(cost, n-2));
}
private int minCost(int[] cost, int n) {
if (n < 0) return 0;
if (n==0 || n==1) return cost[n];
if (dp[n] != 0) return dp[n];
dp[n] = cost[n] + Math.min(minCost(cost, n-1), minCost(cost, n-2));
return dp[n];
}
}
*/