Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions leetcode/3601-3700/3693.Climbing-Stairs-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,62 @@
# [3693.Climbing Stairs II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are climbing a staircase with `n + 1` steps, numbered from 0 to `n`.

You are also given a **1-indexed** integer array `costs` of length `n`, where `costs[i]` is the cost of step `i`.

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`

You start from step 0 with `cost = 0`.

Return the **minimum** total cost to reach step `n`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 4, costs = [1,2,3,4]

Output: 13

Explanation:

One optimal path is 0 → 1 → 2 → 4

Jump Cost Calculation Cost
0 → 1 costs[1] + (1 - 0)2 = 1 + 1 2
1 → 2 costs[2] + (2 - 1)2 = 2 + 1 3
2 → 4 costs[4] + (4 - 2)2 = 4 + 4 8
Thus, the minimum total cost is 2 + 3 + 8 = 13
```

## 题意
> ...
**Example 2:**

## 题解
```
Input: n = 4, costs = [5,1,6,2]

### 思路1
> ...
Climbing Stairs II
```go
Output: 11

Explanation:

One optimal path is 0 → 2 → 4

Jump Cost Calculation Cost
0 → 2 costs[2] + (2 - 0)2 = 1 + 4 5
2 → 4 costs[4] + (4 - 2)2 = 2 + 4 6
Thus, the minimum total cost is 5 + 6 = 11
```

**Example 3:**

```
Input: n = 3, costs = [9,8,3]

Output: 12

Explanation:

The optimal path is 0 → 3 with total cost = costs[3] + (3 - 0)2 = 3 + 9 = 12
```

## 结语

Expand Down
21 changes: 19 additions & 2 deletions leetcode/3601-3700/3693.Climbing-Stairs-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package Solution

func Solution(x bool) bool {
return x
const INF = 0x7fffffff

func Solution(n int, costs []int) int {
dp := make([]int, n+1)
for i := 1; i <= n; i++ {
dp[i] = INF
}
for i := 1; i <= n; i++ {
if i >= 1 {
dp[i] = min(dp[i], dp[i-1]+costs[i-1]+1)
}
if i >= 2 {
dp[i] = min(dp[i], dp[i-2]+costs[i-1]+4)
}
if i >= 3 {
dp[i] = min(dp[i], dp[i-3]+costs[i-1]+9)
}
}
return dp[n]
}
21 changes: 11 additions & 10 deletions leetcode/3601-3700/3693.Climbing-Stairs-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n int
costs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, []int{1, 2, 3, 4}, 13},
{"TestCase2", 4, []int{5, 1, 6, 2}, 11},
{"TestCase3", 3, []int{9, 8, 3}, 12},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.costs)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.costs)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading