Skip to content

Commit 296f2db

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0746
1 parent 57ee9b4 commit 296f2db

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

solution/0700-0799/0746.Min Cost Climbing Stairs/README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,32 @@
4545
<!-- 这里可写当前语言的特殊实现逻辑 -->
4646

4747
```python
48-
48+
class Solution:
49+
def minCostClimbingStairs(self, cost: List[int]) -> int:
50+
pre = cur = 0
51+
n = len(cost)
52+
for i in range(1, n):
53+
t = min(cost[i] + cur, cost[i - 1] + pre)
54+
pre, cur = cur, t
55+
return cur
4956
```
5057

5158
### **Java**
5259

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

5562
```java
56-
63+
class Solution {
64+
public int minCostClimbingStairs(int[] cost) {
65+
int pre = 0, cur = 0;
66+
for (int i = 1, n = cost.length; i < n; ++i) {
67+
int t = Math.min(cost[i] + cur, cost[i - 1] + pre);
68+
pre = cur;
69+
cur = t;
70+
}
71+
return cur;
72+
}
73+
}
5774
```
5875

5976
### **...**

solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,30 @@ Once you pay the cost, you can either climb one or two steps. You need to find m
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def minCostClimbingStairs(self, cost: List[int]) -> int:
66+
pre = cur = 0
67+
n = len(cost)
68+
for i in range(1, n):
69+
t = min(cost[i] + cur, cost[i - 1] + pre)
70+
pre, cur = cur, t
71+
return cur
6572
```
6673

6774
### **Java**
6875

6976
```java
70-
77+
class Solution {
78+
public int minCostClimbingStairs(int[] cost) {
79+
int pre = 0, cur = 0;
80+
for (int i = 1, n = cost.length; i < n; ++i) {
81+
int t = Math.min(cost[i] + cur, cost[i - 1] + pre);
82+
pre = cur;
83+
cur = t;
84+
}
85+
return cur;
86+
}
87+
}
7188
```
7289

7390
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution {
22
public int minCostClimbingStairs(int[] cost) {
3-
int pre1 = 0, pre2 = 0;
4-
int res = 0;
5-
for (int i = 2; i <= cost.length; ++i) {
6-
res = Math.min(pre1 + cost[i - 1], pre2 + cost[i - 2]);
7-
pre2 = pre1;
8-
pre1 = res;
3+
int pre = 0, cur = 0;
4+
for (int i = 1, n = cost.length; i < n; ++i) {
5+
int t = Math.min(cost[i] + cur, cost[i - 1] + pre);
6+
pre = cur;
7+
cur = t;
98
}
10-
return res;
9+
return cur;
1110
}
12-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def minCostClimbingStairs(self, cost: List[int]) -> int:
3+
pre = cur = 0
4+
n = len(cost)
5+
for i in range(1, n):
6+
t = min(cost[i] + cur, cost[i - 1] + pre)
7+
pre, cur = cur, t
8+
return cur

0 commit comments

Comments
 (0)