Skip to content

Commit 2368135

Browse files
committed
Add go solution for 746. Min Cost Climbing Stairs
746. Min Cost Climbing Stairs: https://leetcode.com/problems/min-cost-climbing-stairs/
1 parent b04eada commit 2368135

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

go/min-cost-climbing-stairs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func minCostClimbingStairs(cost []int) int {
4+
size := len(cost)
5+
prev2, prev1, prev0 := 0, 0, 0
6+
for i := 2; i <= size; i++ {
7+
n1, n2 := prev2+cost[i-2], prev1+cost[i-1]
8+
if n1 > n2 {
9+
prev0 = n2
10+
} else {
11+
prev0 = n1
12+
}
13+
prev2, prev1 = prev1, prev0
14+
}
15+
return prev0
16+
}

go/min-cost-climbing-stairs_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func TestMinCostClimbingStairs(t *testing.T) {
6+
if minCostClimbingStairs([]int{10, 15, 20}) != 15 {
7+
t.Fatal()
8+
}
9+
if minCostClimbingStairs([]int{1, 100, 1, 1, 1, 100, 1, 1, 100, 1}) != 6 {
10+
t.Fatal()
11+
}
12+
}

0 commit comments

Comments
 (0)