题解 | #最小花费爬楼梯# | Golang
最小花费爬楼梯
https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e
package main
import (
"fmt"
)
func min(a int, b int) int {
if a < b {
return a
}
return b
}
func minCosts(costs []int, n int) int {
c := 0
if n <= 1 {
return c
}
one_cost, two_cost := costs[0], costs[1]
for i:=2; i<n; i++ {
c = min(one_cost, two_cost)
one_cost = two_cost
two_cost = c + costs[i]
}
return min(one_cost, two_cost)
}
func main() {
n:=0
costs := []int{}
fmt.Scan(&n)
for i:=1;i<=n;i++ {
num := 0
fmt.Scan(&num)
costs = append(costs, num)
}
fmt.Print(minCosts(costs, n))
}
查看1道真题和解析