You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `to(i)` / `dp[i]` represents the cost **to** i-th step:
13
+
* To `0` or `1` step cost 0, because we start from index `0` or `1`.
14
+
* The cost to `i-th` step will be the minimum of `to(i - 1) + cost[i - 1]` and `to(i - 2) + cost[i - 2]`.
6
15
7
16
```kotlin
17
+
// Recursive with memoization
18
+
privateval memo = hashMapOf<Int, Int>()
19
+
8
20
funminCostClimbingStairs(cost:IntArray): Int {
9
-
if (cost.size ==2) return min(cost[0], cost[1])
21
+
return to(cost, cost.size)
22
+
}
23
+
24
+
privatefunto(cost:IntArray, i:Int): Int {
25
+
if (i ==0|| i ==1) return0
26
+
if (memo.containsKey(i)) return memo[i]!!
27
+
memo[i] = min(to(cost, i -1) + cost[i -1], to(cost, i -2) + cost[i -2])
28
+
return memo[i]!!
29
+
}
10
30
31
+
// Bottom-Up DP
32
+
funminCostClimbingStairs(cost:IntArray): Int {
11
33
val n = cost.size
12
34
val dp =IntArray(n +1)
13
35
dp[0] =0
@@ -19,26 +41,27 @@ fun minCostClimbingStairs(cost: IntArray): Int {
19
41
}
20
42
```
21
43
22
-
Or we can represent `dp[i]` to be the cost that we pay in order to climb **from** i-th step. That is, to climbing from i-th step, we have to pay `dp[i] = cost[i] + min(dp[i - 1], dp[i - 2])`.
44
+
### `From` State
45
+
Or we can represent `dp[i]` to be the cost that we pay in order to climb **from**`i-th` step. We have to pay `dp[i] = cost[i] + min(dp[i - 1], dp[i - 2])`.
23
46
24
47
```kotlin
48
+
// Time Complexity: O(n)
49
+
// Space Complexity: O(n)
25
50
funminCostClimbingStairs(cost:IntArray): Int {
26
51
val n = cost.size
27
52
val dp =IntArray(n +1)
28
53
dp[0] = cost[0]
29
54
dp[1] = cost[1]
30
-
for (i in2..n) {
55
+
for (i in2 until n) {
31
56
dp[i] = cost[i] + min(dp[i -1], dp[i -2])
32
57
}
58
+
// To n-th step, we can either start from n - 1 or n - 2.
33
59
return min(dp[i -1], dp[i -2])
34
60
}
35
-
```
36
61
37
-
***Time Complexity**: `O(n)` for one for-loop.
38
-
***Space Complexity**: `O(n)` for `dp` array.
39
-
40
-
### Bottom-DP (Space Optimization)
41
-
```kotlin
62
+
// Space Optimization
63
+
// Time Complexity: O(n)
64
+
// Space Complexity: O(1)
42
65
funminCostClimbingStairs(cost:IntArray): Int {
43
66
if (cost.size ==2) return min(cost[0], cost[1])
44
67
@@ -54,5 +77,23 @@ fun minCostClimbingStairs(cost: IntArray): Int {
Copy file name to clipboardExpand all lines: topics/complexity.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -78,14 +78,14 @@ fun insertSort(A) { // ---- Times ----
78
78
This worst running time can expressed as `a*n^2 + b*n + c`, it's a **quadratic function** of `n`.
79
79
80
80
We shall usually conccentrate on the worst-case running time, because of the three reasons:
81
-
1. It's the upper bound of running time for any input, it gives us a guarantee that the algorithm will never tak any longer.
81
+
1. It's the upper bound of running time for any input, it gives us a guarantee that the algorithm will never take any longer.
82
82
2. For some algorithm, the worst case occurs fairly often.
83
83
3. The "average case" is often roughly as bad as the worst case.
84
84
85
85
We expect that the same algorithm running on a fast machine will run faster than the same algorithm on a slow one, however, we'd like to be able to compare without worrying about how fast the machine is, so we compare the running time based on *asymptotic performance* (the abstraction way) relative to the input size.
86
86
87
87
## Asymptotic Notation
88
-
When look at input sizes large engough to make only the order of growth of the running time relevant, we use *asymptotic notation* to express the **rate of growth** of an algorithm's running time in terms of the input size `n`.
88
+
When we look at input sizes large engough to make only the order of growth of the running time relevant, we use *asymptotic notation* to express the **rate of growth** of an algorithm's running time in terms of the input size `n`.
89
89
90
90
For the worst-case running time of *insert sort* is `a*n^2 + b*n + c`, what we care about is the **order of growth**, we therefore consider:
0 commit comments