Skip to content

Commit ae50856

Browse files
committed
feat: add solutions to lc/lcof2 problems
lc No.0746 & lcof2 No.088. Min Cost Climbing Stairs
1 parent 0a7d9be commit ae50856

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

lcof2/剑指 Offer II 088. 爬楼梯的最少成本/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,38 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:动态规划**
49+
50+
定义 `dp[i]` 表示到达第 `i` 个台阶的最小花费。可以得到状态转移方程:
51+
52+
$$
53+
dp[i] = \min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
54+
$$
55+
56+
最终结果为 `dp[n]`。其中 $n$ 表示 `cost` 数组的长度。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
59+
60+
由于 `dp[i]` 只跟 `dp[i-1]``dp[i-2]` 有关,因此我们还可以对空间进行优化,只用两个变量 `a`, `b` 来记录。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
63+
4864
<!-- tabs:start -->
4965

5066
### **Python3**
5167

5268
<!-- 这里可写当前语言的特殊实现逻辑 -->
5369

70+
```python
71+
class Solution:
72+
def minCostClimbingStairs(self, cost: List[int]) -> int:
73+
n = len(cost)
74+
dp = [0] * (n + 1)
75+
for i in range(2, n + 1):
76+
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
77+
return dp[-1]
78+
```
79+
5480
```python
5581
class Solution:
5682
def minCostClimbingStairs(self, cost: List[int]) -> int:
@@ -64,6 +90,19 @@ class Solution:
6490

6591
<!-- 这里可写当前语言的特殊实现逻辑 -->
6692

93+
```java
94+
class Solution {
95+
public int minCostClimbingStairs(int[] cost) {
96+
int n = cost.length;
97+
int[] dp = new int[n + 1];
98+
for (int i = 2; i <= n; ++i) {
99+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
100+
}
101+
return dp[n];
102+
}
103+
}
104+
```
105+
67106
```java
68107
class Solution {
69108
public int minCostClimbingStairs(int[] cost) {
@@ -80,6 +119,17 @@ class Solution {
80119

81120
### **TypeScript**
82121

122+
```ts
123+
function minCostClimbingStairs(cost: number[]): number {
124+
const n = cost.length;
125+
const dp = new Array(n + 1).fill(0);
126+
for (let i = 2; i <= n; ++i) {
127+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
128+
}
129+
return dp[n];
130+
}
131+
```
132+
83133
```ts
84134
function minCostClimbingStairs(cost: number[]): number {
85135
let a = 0,
@@ -93,6 +143,20 @@ function minCostClimbingStairs(cost: number[]): number {
93143

94144
### **C++**
95145

146+
```cpp
147+
class Solution {
148+
public:
149+
int minCostClimbingStairs(vector<int>& cost) {
150+
int n = cost.size();
151+
vector<int> dp(n + 1);
152+
for (int i = 2; i <= n; ++i) {
153+
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
154+
}
155+
return dp[n];
156+
}
157+
};
158+
```
159+
96160
```cpp
97161
class Solution {
98162
public:
@@ -110,6 +174,24 @@ public:
110174

111175
### **Go**
112176

177+
```go
178+
func minCostClimbingStairs(cost []int) int {
179+
n := len(cost)
180+
dp := make([]int, n+1)
181+
for i := 2; i <= n; i++ {
182+
dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])
183+
}
184+
return dp[n]
185+
}
186+
187+
func min(a, b int) int {
188+
if a < b {
189+
return a
190+
}
191+
return b
192+
}
193+
```
194+
113195
```go
114196
func minCostClimbingStairs(cost []int) int {
115197
a, b := 0, 0

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

+82
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,38 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:动态规划**
56+
57+
定义 `dp[i]` 表示到达第 `i` 个台阶的最小花费。可以得到状态转移方程:
58+
59+
$$
60+
dp[i] = \min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
61+
$$
62+
63+
最终结果为 `dp[n]`。其中 $n$ 表示 `cost` 数组的长度。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
66+
67+
由于 `dp[i]` 只跟 `dp[i-1]``dp[i-2]` 有关,因此我们还可以对空间进行优化,只用两个变量 `a`, `b` 来记录。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
70+
5571
<!-- tabs:start -->
5672

5773
### **Python3**
5874

5975
<!-- 这里可写当前语言的特殊实现逻辑 -->
6076

77+
```python
78+
class Solution:
79+
def minCostClimbingStairs(self, cost: List[int]) -> int:
80+
n = len(cost)
81+
dp = [0] * (n + 1)
82+
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]
85+
```
86+
6187
```python
6288
class Solution:
6389
def minCostClimbingStairs(self, cost: List[int]) -> int:
@@ -71,6 +97,19 @@ class Solution:
7197

7298
<!-- 这里可写当前语言的特殊实现逻辑 -->
7399

100+
```java
101+
class Solution {
102+
public int minCostClimbingStairs(int[] cost) {
103+
int n = cost.length;
104+
int[] dp = new int[n + 1];
105+
for (int i = 2; i <= n; ++i) {
106+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
107+
}
108+
return dp[n];
109+
}
110+
}
111+
```
112+
74113
```java
75114
class Solution {
76115
public int minCostClimbingStairs(int[] cost) {
@@ -87,6 +126,17 @@ class Solution {
87126

88127
### **TypeScript**
89128

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+
90140
```ts
91141
function minCostClimbingStairs(cost: number[]): number {
92142
let a = 0,
@@ -100,6 +150,20 @@ function minCostClimbingStairs(cost: number[]): number {
100150

101151
### **C++**
102152

153+
```cpp
154+
class Solution {
155+
public:
156+
int minCostClimbingStairs(vector<int>& cost) {
157+
int n = cost.size();
158+
vector<int> dp(n + 1);
159+
for (int i = 2; i <= n; ++i) {
160+
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
161+
}
162+
return dp[n];
163+
}
164+
};
165+
```
166+
103167
```cpp
104168
class Solution {
105169
public:
@@ -117,6 +181,24 @@ public:
117181

118182
### **Go**
119183

184+
```go
185+
func minCostClimbingStairs(cost []int) int {
186+
n := len(cost)
187+
dp := make([]int, n+1)
188+
for i := 2; i <= n; i++ {
189+
dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])
190+
}
191+
return dp[n]
192+
}
193+
194+
func min(a, b int) int {
195+
if a < b {
196+
return a
197+
}
198+
return b
199+
}
200+
```
201+
120202
```go
121203
func minCostClimbingStairs(cost []int) int {
122204
a, b := 0, 0

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

+66
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ The total cost is 6.
5050

5151
### **Python3**
5252

53+
```python
54+
class Solution:
55+
def minCostClimbingStairs(self, cost: List[int]) -> int:
56+
n = len(cost)
57+
dp = [0] * (n + 1)
58+
for i in range(2, n + 1):
59+
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
60+
return dp[-1]
61+
```
62+
5363
```python
5464
class Solution:
5565
def minCostClimbingStairs(self, cost: List[int]) -> int:
@@ -61,6 +71,19 @@ class Solution:
6171

6272
### **Java**
6373

74+
```java
75+
class Solution {
76+
public int minCostClimbingStairs(int[] cost) {
77+
int n = cost.length;
78+
int[] dp = new int[n + 1];
79+
for (int i = 2; i <= n; ++i) {
80+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
81+
}
82+
return dp[n];
83+
}
84+
}
85+
```
86+
6487
```java
6588
class Solution {
6689
public int minCostClimbingStairs(int[] cost) {
@@ -77,6 +100,17 @@ class Solution {
77100

78101
### **TypeScript**
79102

103+
```ts
104+
function minCostClimbingStairs(cost: number[]): number {
105+
const n = cost.length;
106+
const dp = new Array(n + 1).fill(0);
107+
for (let i = 2; i <= n; ++i) {
108+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
109+
}
110+
return dp[n];
111+
}
112+
```
113+
80114
```ts
81115
function minCostClimbingStairs(cost: number[]): number {
82116
let a = 0,
@@ -90,6 +124,20 @@ function minCostClimbingStairs(cost: number[]): number {
90124

91125
### **C++**
92126

127+
```cpp
128+
class Solution {
129+
public:
130+
int minCostClimbingStairs(vector<int>& cost) {
131+
int n = cost.size();
132+
vector<int> dp(n + 1);
133+
for (int i = 2; i <= n; ++i) {
134+
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
135+
}
136+
return dp[n];
137+
}
138+
};
139+
```
140+
93141
```cpp
94142
class Solution {
95143
public:
@@ -107,6 +155,24 @@ public:
107155

108156
### **Go**
109157

158+
```go
159+
func minCostClimbingStairs(cost []int) int {
160+
n := len(cost)
161+
dp := make([]int, n+1)
162+
for i := 2; i <= n; i++ {
163+
dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])
164+
}
165+
return dp[n]
166+
}
167+
168+
func min(a, b int) int {
169+
if a < b {
170+
return a
171+
}
172+
return b
173+
}
174+
```
175+
110176
```go
111177
func minCostClimbingStairs(cost []int) int {
112178
a, b := 0, 0

0 commit comments

Comments
 (0)