更新0746.使用最小花费爬楼梯.md 提供Go版本解法的新思路(dp[i]表示从i层起跳所需要支付的最小费用)

This commit is contained in:
jeffreyjia
2023-03-12 13:07:31 +08:00
parent 549d37a54e
commit 0ccbc1ea53
5 changed files with 58 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/leetcode-master.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/leetcode-master.iml" filepath="$PROJECT_DIR$/.idea/leetcode-master.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -284,6 +284,33 @@ func min(a, b int) int {
return b
}
```
``` GO
第二种思路: dp[i]表示从i层起跳所需要支付的最小费用
递推公式:
i<n :dp[i] = min(dp[i-1],dp[i-2])+cost[i]
i==n:dp[i] = min(dp[i-1],dp[i-2]) (登顶)
func minCostClimbingStairs(cost []int) int {
n := len(cost)
dp := make([]int, n+1)
dp[0], dp[1] = cost[0], cost[1]
for i := 2; i <= n; i++ {
if i < n {
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
} else {
dp[i] = min(dp[i-1], dp[i-2])
}
}
return dp[n]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
```
### Javascript
```Javascript