Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dd699e9

Browse files
authored
feat: add solutions to lc problems: No.0746,0747 (doocs#2109)
* No.0746.Min Cost Climbing Stairs * No.0747.Largest Number At Least Twice of Others
1 parent 894bb7d commit dd699e9

File tree

16 files changed

+446
-318
lines changed

16 files changed

+446
-318
lines changed

solution/0700-0799/0746.Min Cost Climbing Stairs/README.md

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@
5454

5555
**方法一:动态规划**
5656

57-
定义 `dp[i]` 表示到达第 `i` 个台阶的最小花费。可以得到状态转移方程:
57+
我们定义 $f[i]$ 表示到达第 $i$ 个阶梯所需要的最小花费,初始时 $f[0] = f[1] = 0$,答案即为 $f[n]$。
58+
59+
当 $i \ge 2$ 时,我们可以从第 $i - 1$ 个阶梯使用 $1$ 步直接到达第 $i$ 个阶梯,或者从第 $i - 2$ 个阶梯使用 $2$ 步到达第 $i$ 个阶梯,因此我们有状态转移方程:
5860

5961
$$
60-
dp[i] = \min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
62+
f[i] = \min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2])
6163
$$
6264

63-
最终结果为 `dp[n]`。其中 $n$ 表示 `cost` 数组的长度。
64-
65-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
65+
最终的答案即为 $f[n]$。
6666

67-
由于 `dp[i]` 只跟 `dp[i-1]``dp[i-2]` 有关,因此我们还可以对空间进行优化,只用两个变量 `a`, `b` 来记录
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `cost` 的长度
6868

69-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
69+
我们注意到,状态转移方程中的 $f[i]$ 只和 $f[i - 1]$ 与 $f[i - 2]$ 有关,因此我们可以使用两个变量 $f$ 和 $g$ 交替地记录 $f[i - 2]$ 和 $f[i - 1]$ 的值,这样空间复杂度可以优化到 $O(1)$。
7070

7171
<!-- tabs:start -->
7272

@@ -78,19 +78,19 @@ $$
7878
class Solution:
7979
def minCostClimbingStairs(self, cost: List[int]) -> int:
8080
n = len(cost)
81-
dp = [0] * (n + 1)
81+
f = [0] * (n + 1)
8282
for i in range(2, n + 1):
83-
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
84-
return dp[-1]
83+
f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1])
84+
return f[n]
8585
```
8686

8787
```python
8888
class Solution:
8989
def minCostClimbingStairs(self, cost: List[int]) -> int:
90-
a = b = 0
91-
for i in range(1, len(cost)):
92-
a, b = b, min(a + cost[i - 1], b + cost[i])
93-
return b
90+
f = g = 0
91+
for i in range(2, len(cost) + 1):
92+
f, g = g, min(f + cost[i - 2], g + cost[i - 1])
93+
return g
9494
```
9595

9696
### **Java**
@@ -101,50 +101,26 @@ class Solution:
101101
class Solution {
102102
public int minCostClimbingStairs(int[] cost) {
103103
int n = cost.length;
104-
int[] dp = new int[n + 1];
104+
int[] f = new int[n + 1];
105105
for (int i = 2; i <= n; ++i) {
106-
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
106+
f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
107107
}
108-
return dp[n];
108+
return f[n];
109109
}
110110
}
111111
```
112112

113113
```java
114114
class Solution {
115115
public int minCostClimbingStairs(int[] cost) {
116-
int a = 0, b = 0;
117-
for (int i = 1; i < cost.length; ++i) {
118-
int c = Math.min(a + cost[i - 1], b + cost[i]);
119-
a = b;
120-
b = c;
116+
int f = 0, g = 0;
117+
for (int i = 2; i <= cost.length; ++i) {
118+
int gg = Math.min(f + cost[i - 2], g + cost[i - 1]);
119+
f = g;
120+
g = gg;
121121
}
122-
return b;
123-
}
124-
}
125-
```
126-
127-
### **TypeScript**
128-
129-
```ts
130-
function minCostClimbingStairs(cost: number[]): number {
131-
const n = cost.length;
132-
const dp = new Array(n + 1).fill(0);
133-
for (let i = 2; i <= n; ++i) {
134-
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
135-
}
136-
return dp[n];
137-
}
138-
```
139-
140-
```ts
141-
function minCostClimbingStairs(cost: number[]): number {
142-
let a = 0,
143-
b = 0;
144-
for (let i = 1; i < cost.length; ++i) {
145-
[a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])];
122+
return g;
146123
}
147-
return b;
148124
}
149125
```
150126

@@ -155,11 +131,11 @@ class Solution {
155131
public:
156132
int minCostClimbingStairs(vector<int>& cost) {
157133
int n = cost.size();
158-
vector<int> dp(n + 1);
134+
vector<int> f(n + 1);
159135
for (int i = 2; i <= n; ++i) {
160-
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
136+
f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
161137
}
162-
return dp[n];
138+
return f[n];
163139
}
164140
};
165141
```
@@ -168,13 +144,13 @@ public:
168144
class Solution {
169145
public:
170146
int minCostClimbingStairs(vector<int>& cost) {
171-
int a = 0, b = 0;
172-
for (int i = 1; i < cost.size(); ++i) {
173-
int c = min(a + cost[i - 1], b + cost[i]);
174-
a = b;
175-
b = c;
147+
int f = 0, g = 0;
148+
for (int i = 2; i <= cost.size(); ++i) {
149+
int gg = min(f + cost[i - 2], g + cost[i - 1]);
150+
f = g;
151+
g = gg;
176152
}
177-
return b;
153+
return g;
178154
}
179155
};
180156
```
@@ -184,21 +160,74 @@ public:
184160
```go
185161
func minCostClimbingStairs(cost []int) int {
186162
n := len(cost)
187-
dp := make([]int, n+1)
163+
f := make([]int, n+1)
188164
for i := 2; i <= n; i++ {
189-
dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])
165+
f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2])
190166
}
191-
return dp[n]
167+
return f[n]
192168
}
193169
```
194170

195171
```go
196172
func minCostClimbingStairs(cost []int) int {
197-
a, b := 0, 0
198-
for i := 1; i < len(cost); i++ {
199-
a, b = b, min(a+cost[i-1], b+cost[i])
173+
var f, g int
174+
for i := 2; i <= n; i++ {
175+
f, g = g, min(f+cost[i-2], g+cost[i-1])
200176
}
201-
return b
177+
return g
178+
}
179+
```
180+
181+
### **TypeScript**
182+
183+
```ts
184+
function minCostClimbingStairs(cost: number[]): number {
185+
const n = cost.length;
186+
const f: number[] = Array(n + 1).fill(0);
187+
for (let i = 2; i <= n; ++i) {
188+
f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]);
189+
}
190+
return f[n];
191+
}
192+
```
193+
194+
```ts
195+
function minCostClimbingStairs(cost: number[]): number {
196+
let a = 0,
197+
b = 0;
198+
for (let i = 1; i < cost.length; ++i) {
199+
[a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])];
200+
}
201+
return b;
202+
}
203+
```
204+
205+
### **Rust**
206+
207+
```rust
208+
impl Solution {
209+
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
210+
let n = cost.len();
211+
let mut f = vec![0; n + 1];
212+
for i in 2..=n {
213+
f[i] = std::cmp::min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
214+
}
215+
f[n]
216+
}
217+
}
218+
```
219+
220+
```rust
221+
impl Solution {
222+
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
223+
let (mut f, mut g) = (0, 0);
224+
for i in 2..=cost.len() {
225+
let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]);
226+
f = g;
227+
g = gg;
228+
}
229+
g
230+
}
202231
}
203232
```
204233

0 commit comments

Comments
 (0)