Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 27, 2023
Merged
Changes from 1 commit
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
Next Next commit
Update 0746.使用最小花费爬楼梯.md
  • Loading branch information
jianghongcheng authored Jun 12, 2023
commit 50ed10475df9c62dae9e149a867a6f083a0dde04
28 changes: 28 additions & 0 deletions problems/0746.使用最小花费爬楼梯.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,35 @@ class Solution:
return dp1 # 返回到达楼顶的最小花费

```
动态规划(版本三)

```python
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp = [0] * len(cost)
dp[0] = cost[0] # 第一步有花费
dp[1] = cost[1]
for i in range(2, len(cost)):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
# 注意最后一步可以理解为不用花费,所以取倒数第一步,第二步的最少值
return min(dp[-1], dp[-2])

```
动态规划(版本四)

```python
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
prev_1 = cost[0] # 前一步的最小花费
prev_2 = cost[1] # 前两步的最小花费
for i in range(2, n):
current = min(prev_1, prev_2) + cost[i] # 当前位置的最小花费
prev_1, prev_2 = prev_2, current # 更新前一步和前两步的最小花费
return min(prev_1, prev_2) # 最后一步可以理解为不用花费,取倒数第一步和第二步的最少值


```
### Go
```Go
func minCostClimbingStairs(cost []int) int {
Expand Down

AltStyle によって変換されたページ (->オリジナル) /