Skip to content

Commit ad45f3f

Browse files
committed
feat: add solutions to lcof2 problem: No.88
lcof2 No.88.Min Cost Climbing Stairs
1 parent ecb8f6e commit ad45f3f

File tree

8 files changed

+142
-31
lines changed

8 files changed

+142
-31
lines changed

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

+65-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
<p><meta charset="UTF-8" />注意:本题与主站 746&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/min-cost-climbing-stairs/">https://leetcode-cn.com/problems/min-cost-climbing-stairs/</a></p>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
@@ -53,15 +52,79 @@
5352
<!-- 这里可写当前语言的特殊实现逻辑 -->
5453

5554
```python
56-
55+
class Solution:
56+
def minCostClimbingStairs(self, cost: List[int]) -> int:
57+
a = b = 0
58+
for i in range(1, len(cost)):
59+
a, b = b, min(a + cost[i - 1], b + cost[i])
60+
return b
5761
```
5862

5963
### **Java**
6064

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

6367
```java
68+
class Solution {
69+
public int minCostClimbingStairs(int[] cost) {
70+
int a = 0, b = 0;
71+
for (int i = 1; i < cost.length; ++i) {
72+
int c = Math.min(a + cost[i - 1], b + cost[i]);
73+
a = b;
74+
b = c;
75+
}
76+
return b;
77+
}
78+
}
79+
```
80+
81+
### **TypeScript**
82+
83+
```ts
84+
function minCostClimbingStairs(cost: number[]): number {
85+
let a = 0,
86+
b = 0;
87+
for (let i = 1; i < cost.length; ++i) {
88+
[a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])];
89+
}
90+
return b;
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int minCostClimbingStairs(vector<int>& cost) {
100+
int a = 0, b = 0;
101+
for (int i = 1; i < cost.size(); ++i) {
102+
int c = min(a + cost[i - 1], b + cost[i]);
103+
a = b;
104+
b = c;
105+
}
106+
return b;
107+
}
108+
};
109+
```
64110
111+
### **Go**
112+
113+
```go
114+
func minCostClimbingStairs(cost []int) int {
115+
a, b := 0, 0
116+
for i := 1; i < len(cost); i++ {
117+
a, b = b, min(a+cost[i-1], b+cost[i])
118+
}
119+
return b
120+
}
121+
122+
func min(a, b int) int {
123+
if a < b {
124+
return a
125+
}
126+
return b
127+
}
65128
```
66129

67130
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int minCostClimbingStairs(vector<int>& cost) {
4+
int a = 0, b = 0;
5+
for (int i = 1; i < cost.size(); ++i) {
6+
int c = min(a + cost[i - 1], b + cost[i]);
7+
a = b;
8+
b = c;
9+
}
10+
return b;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func minCostClimbingStairs(cost []int) int {
2+
a, b := 0, 0
3+
for i := 1; i < len(cost); i++ {
4+
a, b = b, min(a+cost[i-1], b+cost[i])
5+
}
6+
return b
7+
}
8+
9+
func min(a, b int) int {
10+
if a < b {
11+
return a
12+
}
13+
return b
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int minCostClimbingStairs(int[] cost) {
3+
int a = 0, b = 0;
4+
for (int i = 1; i < cost.length; ++i) {
5+
int c = Math.min(a + cost[i - 1], b + cost[i]);
6+
a = b;
7+
b = c;
8+
}
9+
return b;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def minCostClimbingStairs(self, cost: List[int]) -> int:
3+
a = b = 0
4+
for i in range(1, len(cost)):
5+
a, b = b, min(a + cost[i - 1], b + cost[i])
6+
return b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function minCostClimbingStairs(cost: number[]): number {
2+
let a = 0, b = 0;
3+
for (let i = 1; i < cost.length; ++i) {
4+
[a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])];
5+
}
6+
return b;
7+
};

solution/README.md

+16-17
Large diffs are not rendered by default.

solution/README_EN.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,6 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
707707
| [0696](https://leetcode.com/problems/count-binary-substrings) | [Count Binary Substrings](/solution/0600-0699/0696.Count%20Binary%20Substrings/README_EN.md) | `Two Pointers`,`String` | Easy | |
708708
| [0697](https://leetcode.com/problems/degree-of-an-array) | [Degree of an Array](/solution/0600-0699/0697.Degree%20of%20an%20Array/README_EN.md) | `Array`,`Hash Table` | Easy | |
709709
| [0698](https://leetcode.com/problems/partition-to-k-equal-sum-subsets) | [Partition to K Equal Sum Subsets](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README_EN.md) | `Bit Manipulation`,`Memoization`,`Array`,`Dynamic Programming`,`Backtracking`,`Bitmask` | Medium | |
710-
| [0699](https://leetcode.com/problems/falling-squares) | [Falling Squares](/solution/0600-0699/0699.Falling%20Squares/README_EN.md) | `Segment Tree`,`Array`,`Ordered Set` | Hard | |
711710
| [0700](https://leetcode.com/problems/search-in-a-binary-search-tree) | [Search in a Binary Search Tree](/solution/0700-0799/0700.Search%20in%20a%20Binary%20Search%20Tree/README_EN.md) | `Tree`,`Binary Search Tree`,`Binary Tree` | Easy | |
712711
| [0701](https://leetcode.com/problems/insert-into-a-binary-search-tree) | [Insert into a Binary Search Tree](/solution/0700-0799/0701.Insert%20into%20a%20Binary%20Search%20Tree/README_EN.md) | `Tree`,`Binary Search Tree`,`Binary Tree` | Medium | |
713712
| [0702](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size) | [Search in a Sorted Array of Unknown Size](/solution/0700-0799/0702.Search%20in%20a%20Sorted%20Array%20of%20Unknown%20Size/README_EN.md) | `Array`,`Binary Search`,`Interactive` | Medium | 🔒 |
@@ -900,7 +899,6 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
900899
| [0889](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal) | [Construct Binary Tree from Preorder and Postorder Traversal](/solution/0800-0899/0889.Construct%20Binary%20Tree%20from%20Preorder%20and%20Postorder%20Traversal/README_EN.md) | `Tree`,`Array`,`Hash Table`,`Divide and Conquer`,`Binary Tree` | Medium | |
901900
| [0890](https://leetcode.com/problems/find-and-replace-pattern) | [Find and Replace Pattern](/solution/0800-0899/0890.Find%20and%20Replace%20Pattern/README_EN.md) | `Array`,`Hash Table`,`String` | Medium | |
902901
| [0891](https://leetcode.com/problems/sum-of-subsequence-widths) | [Sum of Subsequence Widths](/solution/0800-0899/0891.Sum%20of%20Subsequence%20Widths/README_EN.md) | `Array`,`Math`,`Sorting` | Hard | |
903-
| [0892](https://leetcode.com/problems/surface-area-of-3d-shapes) | [Surface Area of 3D Shapes](/solution/0800-0899/0892.Surface%20Area%20of%203D%20Shapes/README_EN.md) | `Geometry`,`Array`,`Math`,`Matrix` | Easy | |
904902
| [0893](https://leetcode.com/problems/groups-of-special-equivalent-strings) | [Groups of Special-Equivalent Strings](/solution/0800-0899/0893.Groups%20of%20Special-Equivalent%20Strings/README_EN.md) | `Array`,`Hash Table`,`String` | Medium | |
905903
| [0894](https://leetcode.com/problems/all-possible-full-binary-trees) | [All Possible Full Binary Trees](/solution/0800-0899/0894.All%20Possible%20Full%20Binary%20Trees/README_EN.md) | `Tree`,`Recursion`,`Memoization`,`Dynamic Programming`,`Binary Tree` | Medium | |
906904
| [0895](https://leetcode.com/problems/maximum-frequency-stack) | [Maximum Frequency Stack](/solution/0800-0899/0895.Maximum%20Frequency%20Stack/README_EN.md) | `Stack`,`Design`,`Hash Table`,`Ordered Set` | Hard | |
@@ -1673,6 +1671,7 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
16731671
| [1662](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent) | [Check If Two String Arrays are Equivalent](/solution/1600-1699/1662.Check%20If%20Two%20String%20Arrays%20are%20Equivalent/README_EN.md) | `Array`,`String` | Easy | |
16741672
| [1663](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value) | [Smallest String With A Given Numeric Value](/solution/1600-1699/1663.Smallest%20String%20With%20A%20Given%20Numeric%20Value/README_EN.md) | `Greedy`,`String` | Medium | |
16751673
| [1664](https://leetcode.com/problems/ways-to-make-a-fair-array) | [Ways to Make a Fair Array](/solution/1600-1699/1664.Ways%20to%20Make%20a%20Fair%20Array/README_EN.md) | `Array`,`Dynamic Programming` | Medium | |
1674+
| [1665](https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks) | [Minimum Initial Energy to Finish Tasks](/solution/1600-1699/1665.Minimum%20Initial%20Energy%20to%20Finish%20Tasks/README_EN.md) | `Greedy`,`Array`,`Sorting` | Hard | |
16761675
| [1666](https://leetcode.com/problems/change-the-root-of-a-binary-tree) | [Change the Root of a Binary Tree](/solution/1600-1699/1666.Change%20the%20Root%20of%20a%20Binary%20Tree/README_EN.md) | `Tree`,`Depth-First Search`,`Binary Tree` | Medium | 🔒 |
16771676
| [1667](https://leetcode.com/problems/fix-names-in-a-table) | [Fix Names in a Table](/solution/1600-1699/1667.Fix%20Names%20in%20a%20Table/README_EN.md) | `Database` | Easy | 🔒 |
16781677
| [1668](https://leetcode.com/problems/maximum-repeating-substring) | [Maximum Repeating Substring](/solution/1600-1699/1668.Maximum%20Repeating%20Substring/README_EN.md) | `String`,`String Matching` | Easy | |
@@ -2090,16 +2089,16 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
20902089
| [2080](https://leetcode.com/problems/range-frequency-queries) | [Range Frequency Queries](/solution/2000-2099/2080.Range%20Frequency%20Queries/README_EN.md) | `Design`,`Segment Tree`,`Array`,`Hash Table`,`Binary Search` | Medium | |
20912090
| [2081](https://leetcode.com/problems/sum-of-k-mirror-numbers) | [Sum of k-Mirror Numbers](/solution/2000-2099/2081.Sum%20of%20k-Mirror%20Numbers/README_EN.md) | `Math`,`Enumeration` | Hard | |
20922091
| [2082](https://leetcode.com/problems/the-number-of-rich-customers) | [The Number of Rich Customers](/solution/2000-2099/2082.The%20Number%20of%20Rich%20Customers/README_EN.md) | `Database` | Easy | 🔒 |
2093-
| [2083](https://leetcode.com/problems/substrings-that-begin-and-end-with-the-same-letter) | [Substrings That Begin and End With the Same Letter](/solution/2000-2099/2083.Substrings%20That%20Begin%20and%20End%20With%20the%20Same%20Letter/README_EN.md) | | Medium | 🔒 |
2094-
| [2084](https://leetcode.com/problems/drop-type-1-orders-for-customers-with-type-0-orders) | [Drop Type 1 Orders for Customers With Type 0 Orders](/solution/2000-2099/2084.Drop%20Type%201%20Orders%20for%20Customers%20With%20Type%200%20Orders/README_EN.md) | | Medium | 🔒 |
2095-
| [2085](https://leetcode.com/problems/count-common-words-with-one-occurrence) | [Count Common Words With One Occurrence](/solution/2000-2099/2085.Count%20Common%20Words%20With%20One%20Occurrence/README_EN.md) | | Easy | |
2096-
| [2086](https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses) | [Minimum Number of Buckets Required to Collect Rainwater from Houses](/solution/2000-2099/2086.Minimum%20Number%20of%20Buckets%20Required%20to%20Collect%20Rainwater%20from%20Houses/README_EN.md) | | Medium | |
2097-
| [2087](https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid) | [Minimum Cost Homecoming of a Robot in a Grid](/solution/2000-2099/2087.Minimum%20Cost%20Homecoming%20of%20a%20Robot%20in%20a%20Grid/README_EN.md) | | Medium | |
2098-
| [2088](https://leetcode.com/problems/count-fertile-pyramids-in-a-land) | [Count Fertile Pyramids in a Land](/solution/2000-2099/2088.Count%20Fertile%20Pyramids%20in%20a%20Land/README_EN.md) | | Hard | |
2099-
| [2089](https://leetcode.com/problems/find-target-indices-after-sorting-array) | [Find Target Indices After Sorting Array](/solution/2000-2099/2089.Find%20Target%20Indices%20After%20Sorting%20Array/README_EN.md) | | Easy | |
2100-
| [2090](https://leetcode.com/problems/k-radius-subarray-averages) | [K Radius Subarray Averages](/solution/2000-2099/2090.K%20Radius%20Subarray%20Averages/README_EN.md) | | Medium | |
2101-
| [2091](https://leetcode.com/problems/removing-minimum-and-maximum-from-array) | [Removing Minimum and Maximum From Array](/solution/2000-2099/2091.Removing%20Minimum%20and%20Maximum%20From%20Array/README_EN.md) | | Medium | |
2102-
| [2092](https://leetcode.com/problems/find-all-people-with-secret) | [Find All People With Secret](/solution/2000-2099/2092.Find%20All%20People%20With%20Secret/README_EN.md) | | Hard | |
2092+
| [2083](https://leetcode.com/problems/substrings-that-begin-and-end-with-the-same-letter) | [Substrings That Begin and End With the Same Letter](/solution/2000-2099/2083.Substrings%20That%20Begin%20and%20End%20With%20the%20Same%20Letter/README_EN.md) | `Hash Table`,`Math`,`String`,`Counting`,`Prefix Sum` | Medium | 🔒 |
2093+
| [2084](https://leetcode.com/problems/drop-type-1-orders-for-customers-with-type-0-orders) | [Drop Type 1 Orders for Customers With Type 0 Orders](/solution/2000-2099/2084.Drop%20Type%201%20Orders%20for%20Customers%20With%20Type%200%20Orders/README_EN.md) | `Database` | Medium | 🔒 |
2094+
| [2085](https://leetcode.com/problems/count-common-words-with-one-occurrence) | [Count Common Words With One Occurrence](/solution/2000-2099/2085.Count%20Common%20Words%20With%20One%20Occurrence/README_EN.md) | `Array`,`Hash Table`,`String`,`Counting` | Easy | |
2095+
| [2086](https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses) | [Minimum Number of Buckets Required to Collect Rainwater from Houses](/solution/2000-2099/2086.Minimum%20Number%20of%20Buckets%20Required%20to%20Collect%20Rainwater%20from%20Houses/README_EN.md) | `Greedy`,`String`,`Dynamic Programming` | Medium | |
2096+
| [2087](https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid) | [Minimum Cost Homecoming of a Robot in a Grid](/solution/2000-2099/2087.Minimum%20Cost%20Homecoming%20of%20a%20Robot%20in%20a%20Grid/README_EN.md) | `Greedy`,`Array`,`Matrix` | Medium | |
2097+
| [2088](https://leetcode.com/problems/count-fertile-pyramids-in-a-land) | [Count Fertile Pyramids in a Land](/solution/2000-2099/2088.Count%20Fertile%20Pyramids%20in%20a%20Land/README_EN.md) | `Array`,`Dynamic Programming`,`Matrix` | Hard | |
2098+
| [2089](https://leetcode.com/problems/find-target-indices-after-sorting-array) | [Find Target Indices After Sorting Array](/solution/2000-2099/2089.Find%20Target%20Indices%20After%20Sorting%20Array/README_EN.md) | `Array`,`Binary Search`,`Sorting` | Easy | |
2099+
| [2090](https://leetcode.com/problems/k-radius-subarray-averages) | [K Radius Subarray Averages](/solution/2000-2099/2090.K%20Radius%20Subarray%20Averages/README_EN.md) | `Array`,`Sliding Window` | Medium | |
2100+
| [2091](https://leetcode.com/problems/removing-minimum-and-maximum-from-array) | [Removing Minimum and Maximum From Array](/solution/2000-2099/2091.Removing%20Minimum%20and%20Maximum%20From%20Array/README_EN.md) | `Greedy`,`Array` | Medium | |
2101+
| [2092](https://leetcode.com/problems/find-all-people-with-secret) | [Find All People With Secret](/solution/2000-2099/2092.Find%20All%20People%20With%20Secret/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Union Find`,`Graph`,`Sorting` | Hard | |
21032102

21042103
## Copyright
21052104

0 commit comments

Comments
 (0)