更新0746.使用最小花费爬楼梯.md 提供Go版本解法的新思路(dp[i]表示从i层起跳所需要支付的最小费用)
This commit is contained in:
parent
549d37a54e
commit
0ccbc1ea53
|
|
@ -0,0 +1,8 @@
|
|||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue