From 477354798173898068f8d9890fbddb353d14796f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 29 Jan 2026 22:49:49 +0800 Subject: [PATCH] Add solution and test-cases for problem 3693 --- .../3693.Climbing-Stairs-II/README.md | 58 +++++++++++++++---- .../3693.Climbing-Stairs-II/Solution.go | 21 ++++++- .../3693.Climbing-Stairs-II/Solution_test.go | 21 +++---- 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/leetcode/3601-3700/3693.Climbing-Stairs-II/README.md b/leetcode/3601-3700/3693.Climbing-Stairs-II/README.md index bdf62a5ee..27428cb6a 100755 --- a/leetcode/3601-3700/3693.Climbing-Stairs-II/README.md +++ b/leetcode/3601-3700/3693.Climbing-Stairs-II/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution.go b/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution.go index d115ccf5e..e407babce 100644 --- a/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution.go +++ b/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution.go @@ -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] } diff --git a/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution_test.go b/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution_test.go index 14ff50eb4..d8f52543c 100644 --- a/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution_test.go +++ b/leetcode/3601-3700/3693.Climbing-Stairs-II/Solution_test.go @@ -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() { }