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

Commit f7a31a2

Browse files
committed
Add: Min Cost Climbing Stairs
1 parent b6252cd commit f7a31a2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
package min_cost_climbing_stairs
2+
3+
func minCostClimbingStairs(cost []int) int {
4+
f1, f2, l := 0, 0, len(cost)-1
5+
for i := l; i >= 0; i-- {
6+
f0 := f1
7+
if f2 < f1 {
8+
f0 = f2
9+
}
10+
f0 += cost[i]
11+
f1, f2 = f0, f1
12+
}
13+
if f1 <= f2 {
14+
return f1
15+
}
16+
return f2
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package min_cost_climbing_stairs
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected int
8+
}
9+
10+
func TestMinCostClimbingStairs(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{10, 15, 20},
14+
expected: 15,
15+
},
16+
{
17+
input: []int{1, 100, 1, 1, 1, 100, 1, 1, 100, 1},
18+
expected: 6,
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := minCostClimbingStairs(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
(0)

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