-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEasy_746_ClimbStairsCost.kt
More file actions
55 lines (45 loc) · 1.24 KB
/
Copy pathEasy_746_ClimbStairsCost.kt
File metadata and controls
55 lines (45 loc) · 1.24 KB
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
package com.boycoder.problems.dp
import com.boycoder.utils.asserts
/**
* @Author: zhutao
* @datetime: 2021/6/3
* @desc:
*/
object Easy_746_ClimbStairsCost {
private var cost: IntArray = intArrayOf()
private var cache = intArrayOf()
fun minCostClimbingStairs(cost: IntArray): Int {
this.cost = cost
cache = IntArray(cost.size + 1)
return min(cost.size)
}
// dp
private fun min(n: Int): Int {
val dp = IntArray(n + 1)
dp[0] = cost[0]
dp[1] = cost[1]
for (i in 2..n) {
val cost = cost.getOrNull(i)?:0
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost
}
return dp[n]
}
/**
* To get to n, we need to pay: Math.min(dfs(n - 1) + cost[n], dfs(n - 2) + cost[n])
*/
private fun min1(n: Int): Int {
if (n == 0) return cost[0]
if (n == 1) return cost[1]
if (cache[n] != 0) {
return cache[n]
}
val v = cost.getOrNull(n)?:0
val res = Math.min(min1(n - 1) + v, min1(n - 2) + v)
cache[n] = res
return res
}
}
fun main() {
val res = Easy_746_ClimbStairsCost.minCostClimbingStairs(intArrayOf(1, 100, 1, 1, 1, 100, 1, 1, 100, 1))
asserts(res, 6)
}