Skip to content

Commit bc5bff0

Browse files
committed
feat: add solutions to lc problem: No.3693
1 parent 3f69ef9 commit bc5bff0

File tree

8 files changed

+294
-8
lines changed

8 files changed

+294
-8
lines changed

solution/3600-3699/3693.Climbing Stairs II/README.md

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,32 +133,134 @@ source: 第 166 场双周赛 Q2
133133

134134
<!-- solution:start -->
135135

136-
### 方法一
136+
### 方法一:动态规划
137+
138+
我们定义 $f[i]$ 表示到达第 $i$ 级台阶所需的最小总成本,初始时 $f[0] = 0$,其余 $f[i] = +\infty$。
139+
140+
对于每一级台阶 $i$,我们可以从第 $i-1$ 级、第 $i-2$ 级或第 $i-3$ 级台阶跳跃过来,因此我们有以下状态转移方程:
141+
142+
$$
143+
f[i] = \min_{j=i-3}^{i-1} (f[j] + \textit{costs}[i - 1] + (i - j)^2)
144+
$$
145+
146+
其中 $\textit{costs}[i]$ 是第 $i$ 级台阶的成本,$(i - j)^2$ 是从第 $j$ 级台阶跳到第 $i$ 级台阶的跳跃成本。注意,我们需要确保 $j$ 不小于 $0$。
147+
148+
最终答案为 $f[n]$。
149+
150+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是台阶的数量。
137151

138152
<!-- tabs:start -->
139153

140154
#### Python3
141155

142156
```python
143-
157+
class Solution:
158+
def climbStairs(self, n: int, costs: List[int]) -> int:
159+
n = len(costs)
160+
f = [inf] * (n + 1)
161+
f[0] = 0
162+
for i, x in enumerate(costs, 1):
163+
for j in range(i - 3, i):
164+
if j >= 0:
165+
f[i] = min(f[i], f[j] + x + (i - j) ** 2)
166+
return f[n]
144167
```
145168

146169
#### Java
147170

148171
```java
149-
172+
class Solution {
173+
public int climbStairs(int n, int[] costs) {
174+
int[] f = new int[n + 1];
175+
final int inf = Integer.MAX_VALUE / 2;
176+
Arrays.fill(f, inf);
177+
f[0] = 0;
178+
for (int i = 1; i <= n; ++i) {
179+
int x = costs[i - 1];
180+
for (int j = Math.max(0, i - 3); j < i; ++j) {
181+
f[i] = Math.min(f[i], f[j] + x + (i - j) * (i - j));
182+
}
183+
}
184+
return f[n];
185+
}
186+
}
150187
```
151188

152189
#### C++
153190

154191
```cpp
155-
192+
class Solution {
193+
public:
194+
int climbStairs(int n, vector<int>& costs) {
195+
vector<int> f(n + 1, INT_MAX / 2);
196+
f[0] = 0;
197+
for (int i = 1; i <= n; ++i) {
198+
int x = costs[i - 1];
199+
for (int j = max(0, i - 3); j < i; ++j) {
200+
f[i] = min(f[i], f[j] + x + (i - j) * (i - j));
201+
}
202+
}
203+
return f[n];
204+
}
205+
};
156206
```
157207
158208
#### Go
159209
160210
```go
211+
func climbStairs(n int, costs []int) int {
212+
const inf = int(1e9)
213+
f := make([]int, n+1)
214+
for i := range f {
215+
f[i] = inf
216+
}
217+
f[0] = 0
218+
for i := 1; i <= n; i++ {
219+
x := costs[i-1]
220+
for j := max(0, i-3); j < i; j++ {
221+
f[i] = min(f[i], f[j]+x+(i-j)*(i-j))
222+
}
223+
}
224+
return f[n]
225+
}
226+
```
227+
228+
#### TypeScript
229+
230+
```ts
231+
function climbStairs(n: number, costs: number[]): number {
232+
const inf = Number.MAX_SAFE_INTEGER / 2;
233+
const f = Array(n + 1).fill(inf);
234+
f[0] = 0;
235+
236+
for (let i = 1; i <= n; ++i) {
237+
const x = costs[i - 1];
238+
for (let j = Math.max(0, i - 3); j < i; ++j) {
239+
f[i] = Math.min(f[i], f[j] + x + (i - j) * (i - j));
240+
}
241+
}
242+
return f[n];
243+
}
244+
```
161245

246+
#### Rust
247+
248+
```rust
249+
impl Solution {
250+
pub fn climb_stairs(n: i32, costs: Vec<i32>) -> i32 {
251+
let n = n as usize;
252+
let inf = i32::MAX / 2;
253+
let mut f = vec![inf; n + 1];
254+
f[0] = 0;
255+
for i in 1..=n {
256+
let x = costs[i - 1];
257+
for j in (i.saturating_sub(3))..i {
258+
f[i] = f[i].min(f[j] + x + ((i - j) * (i - j)) as i32);
259+
}
260+
}
261+
f[n]
262+
}
263+
}
162264
```
163265

164266
<!-- tabs:end -->

solution/3600-3699/3693.Climbing Stairs II/README_EN.md

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,32 +130,134 @@ source: Biweekly Contest 166 Q2
130130

131131
<!-- solution:start -->
132132

133-
### Solution 1
133+
### Solution 1: Dynamic Programming
134+
135+
We define $f[i]$ as the minimum total cost required to reach the $i$-th stair, initially $f[0] = 0$, and all other $f[i] = +\infty$.
136+
137+
For each stair $i$, we can jump from the $(i-1)$-th, $(i-2)$-th, or $(i-3)$-th stair, so we have the following state transition equation:
138+
139+
$$
140+
f[i] = \min_{j=i-3}^{i-1} (f[j] + \textit{costs}[i - 1] + (i - j)^2)
141+
$$
142+
143+
Where $\textit{costs}[i]$ is the cost of the $i$-th stair, and $(i - j)^2$ is the jump cost from the $j$-th stair to the $i$-th stair. Note that we need to ensure $j$ is not less than $0$.
144+
145+
The final answer is $f[n]$.
146+
147+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of stairs.
134148

135149
<!-- tabs:start -->
136150

137151
#### Python3
138152

139153
```python
140-
154+
class Solution:
155+
def climbStairs(self, n: int, costs: List[int]) -> int:
156+
n = len(costs)
157+
f = [inf] * (n + 1)
158+
f[0] = 0
159+
for i, x in enumerate(costs, 1):
160+
for j in range(i - 3, i):
161+
if j >= 0:
162+
f[i] = min(f[i], f[j] + x + (i - j) ** 2)
163+
return f[n]
141164
```
142165

143166
#### Java
144167

145168
```java
146-
169+
class Solution {
170+
public int climbStairs(int n, int[] costs) {
171+
int[] f = new int[n + 1];
172+
final int inf = Integer.MAX_VALUE / 2;
173+
Arrays.fill(f, inf);
174+
f[0] = 0;
175+
for (int i = 1; i <= n; ++i) {
176+
int x = costs[i - 1];
177+
for (int j = Math.max(0, i - 3); j < i; ++j) {
178+
f[i] = Math.min(f[i], f[j] + x + (i - j) * (i - j));
179+
}
180+
}
181+
return f[n];
182+
}
183+
}
147184
```
148185

149186
#### C++
150187

151188
```cpp
152-
189+
class Solution {
190+
public:
191+
int climbStairs(int n, vector<int>& costs) {
192+
vector<int> f(n + 1, INT_MAX / 2);
193+
f[0] = 0;
194+
for (int i = 1; i <= n; ++i) {
195+
int x = costs[i - 1];
196+
for (int j = max(0, i - 3); j < i; ++j) {
197+
f[i] = min(f[i], f[j] + x + (i - j) * (i - j));
198+
}
199+
}
200+
return f[n];
201+
}
202+
};
153203
```
154204
155205
#### Go
156206
157207
```go
208+
func climbStairs(n int, costs []int) int {
209+
const inf = int(1e9)
210+
f := make([]int, n+1)
211+
for i := range f {
212+
f[i] = inf
213+
}
214+
f[0] = 0
215+
for i := 1; i <= n; i++ {
216+
x := costs[i-1]
217+
for j := max(0, i-3); j < i; j++ {
218+
f[i] = min(f[i], f[j]+x+(i-j)*(i-j))
219+
}
220+
}
221+
return f[n]
222+
}
223+
```
224+
225+
#### TypeScript
226+
227+
```ts
228+
function climbStairs(n: number, costs: number[]): number {
229+
const inf = Number.MAX_SAFE_INTEGER / 2;
230+
const f = Array(n + 1).fill(inf);
231+
f[0] = 0;
232+
233+
for (let i = 1; i <= n; ++i) {
234+
const x = costs[i - 1];
235+
for (let j = Math.max(0, i - 3); j < i; ++j) {
236+
f[i] = Math.min(f[i], f[j] + x + (i - j) * (i - j));
237+
}
238+
}
239+
return f[n];
240+
}
241+
```
158242

243+
#### Rust
244+
245+
```rust
246+
impl Solution {
247+
pub fn climb_stairs(n: i32, costs: Vec<i32>) -> i32 {
248+
let n = n as usize;
249+
let inf = i32::MAX / 2;
250+
let mut f = vec![inf; n + 1];
251+
f[0] = 0;
252+
for i in 1..=n {
253+
let x = costs[i - 1];
254+
for j in (i.saturating_sub(3))..i {
255+
f[i] = f[i].min(f[j] + x + ((i - j) * (i - j)) as i32);
256+
}
257+
}
258+
f[n]
259+
}
260+
}
159261
```
160262

161263
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int climbStairs(int n, vector<int>& costs) {
4+
vector<int> f(n + 1, INT_MAX / 2);
5+
f[0] = 0;
6+
for (int i = 1; i <= n; ++i) {
7+
int x = costs[i - 1];
8+
for (int j = max(0, i - 3); j < i; ++j) {
9+
f[i] = min(f[i], f[j] + x + (i - j) * (i - j));
10+
}
11+
}
12+
return f[n];
13+
}
14+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func climbStairs(n int, costs []int) int {
2+
const inf = int(1e9)
3+
f := make([]int, n+1)
4+
for i := range f {
5+
f[i] = inf
6+
}
7+
f[0] = 0
8+
for i := 1; i <= n; i++ {
9+
x := costs[i-1]
10+
for j := max(0, i-3); j < i; j++ {
11+
f[i] = min(f[i], f[j]+x+(i-j)*(i-j))
12+
}
13+
}
14+
return f[n]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int climbStairs(int n, int[] costs) {
3+
int[] f = new int[n + 1];
4+
final int inf = Integer.MAX_VALUE / 2;
5+
Arrays.fill(f, inf);
6+
f[0] = 0;
7+
for (int i = 1; i <= n; ++i) {
8+
int x = costs[i - 1];
9+
for (int j = Math.max(0, i - 3); j < i; ++j) {
10+
f[i] = Math.min(f[i], f[j] + x + (i - j) * (i - j));
11+
}
12+
}
13+
return f[n];
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def climbStairs(self, n: int, costs: List[int]) -> int:
3+
n = len(costs)
4+
f = [inf] * (n + 1)
5+
f[0] = 0
6+
for i, x in enumerate(costs, 1):
7+
for j in range(i - 3, i):
8+
if j >= 0:
9+
f[i] = min(f[i], f[j] + x + (i - j) ** 2)
10+
return f[n]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn climb_stairs(n: i32, costs: Vec<i32>) -> i32 {
3+
let n = n as usize;
4+
let inf = i32::MAX / 2;
5+
let mut f = vec![inf; n + 1];
6+
f[0] = 0;
7+
for i in 1..=n {
8+
let x = costs[i - 1];
9+
for j in (i.saturating_sub(3))..i {
10+
f[i] = f[i].min(f[j] + x + ((i - j) * (i - j)) as i32);
11+
}
12+
}
13+
f[n]
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function climbStairs(n: number, costs: number[]): number {
2+
const inf = Number.MAX_SAFE_INTEGER / 2;
3+
const f = Array(n + 1).fill(inf);
4+
f[0] = 0;
5+
6+
for (let i = 1; i <= n; ++i) {
7+
const x = costs[i - 1];
8+
for (let j = Math.max(0, i - 3); j < i; ++j) {
9+
f[i] = Math.min(f[i], f[j] + x + (i - j) * (i - j));
10+
}
11+
}
12+
return f[n];
13+
}

0 commit comments

Comments
 (0)