Skip to content

Commit 6c1ff0c

Browse files
authored
Merge pull request #1440 from 0xff-dev/3693
Add solution and test-cases for problem 3693
2 parents ae559d3 + 4773547 commit 6c1ff0c

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

leetcode/3601-3700/3693.Climbing-Stairs-II/README.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
11
# [3693.Climbing Stairs II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are climbing a staircase with `n + 1` steps, numbered from 0 to `n`.
5+
6+
You are also given a **1-indexed** integer array `costs` of length `n`, where `costs[i]` is the cost of step `i`.
7+
8+
From step `i`, you can jump **only** to step `i + 1`, `i + 2`, or `i + 3`. The cost of jumping from step `i` to step `j` is defined as: `costs[j] + (j - i)^2`
9+
10+
You start from step 0 with `cost = 0`.
11+
12+
Return the **minimum** total cost to reach step `n`.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: n = 4, costs = [1,2,3,4]
18+
19+
Output: 13
20+
21+
Explanation:
22+
23+
One optimal path is 0 → 1 → 2 → 4
24+
25+
Jump Cost Calculation Cost
26+
0 → 1 costs[1] + (1 - 0)2 = 1 + 1 2
27+
1 → 2 costs[2] + (2 - 1)2 = 2 + 1 3
28+
2 → 4 costs[4] + (4 - 2)2 = 4 + 4 8
29+
Thus, the minimum total cost is 2 + 3 + 8 = 13
1330
```
1431

15-
## 题意
16-
> ...
32+
**Example 2:**
1733

18-
## 题解
34+
```
35+
Input: n = 4, costs = [5,1,6,2]
1936
20-
### 思路1
21-
> ...
22-
Climbing Stairs II
23-
```go
37+
Output: 11
38+
39+
Explanation:
40+
41+
One optimal path is 0 → 2 → 4
42+
43+
Jump Cost Calculation Cost
44+
0 → 2 costs[2] + (2 - 0)2 = 1 + 4 5
45+
2 → 4 costs[4] + (4 - 2)2 = 2 + 4 6
46+
Thus, the minimum total cost is 5 + 6 = 11
2447
```
2548

49+
**Example 3:**
50+
51+
```
52+
Input: n = 3, costs = [9,8,3]
53+
54+
Output: 12
55+
56+
Explanation:
57+
58+
The optimal path is 0 → 3 with total cost = costs[3] + (3 - 0)2 = 3 + 9 = 12
59+
```
2660

2761
## 结语
2862

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const INF = 0x7fffffff
4+
5+
func Solution(n int, costs []int) int {
6+
dp := make([]int, n+1)
7+
for i := 1; i <= n; i++ {
8+
dp[i] = INF
9+
}
10+
for i := 1; i <= n; i++ {
11+
if i >= 1 {
12+
dp[i] = min(dp[i], dp[i-1]+costs[i-1]+1)
13+
}
14+
if i >= 2 {
15+
dp[i] = min(dp[i], dp[i-2]+costs[i-1]+4)
16+
}
17+
if i >= 3 {
18+
dp[i] = min(dp[i], dp[i-3]+costs[i-1]+9)
19+
}
20+
}
21+
return dp[n]
522
}

leetcode/3601-3700/3693.Climbing-Stairs-II/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
costs []int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, []int{1, 2, 3, 4}, 13},
18+
{"TestCase2", 4, []int{5, 1, 6, 2}, 11},
19+
{"TestCase3", 3, []int{9, 8, 3}, 12},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.costs)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.n, c.costs)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)