Skip to content

Commit

Permalink
Merge 6c91626 into 348e922
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz authored Apr 24, 2019
2 parents 348e922 + 6c91626 commit 7da382f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,4 @@ problems from
* [Day 242](https://github.com/vaskoz/dailycodingproblem-go/issues/498)
* [Day 243](https://github.com/vaskoz/dailycodingproblem-go/issues/502)
* [Day 244](https://github.com/vaskoz/dailycodingproblem-go/issues/503)
* [Day 245](https://github.com/vaskoz/dailycodingproblem-go/issues/506)
22 changes: 22 additions & 0 deletions day245/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package day245

// MinimumJumps returns the minimum steps to reach the end.
// Each element contains the max steps that can be taken
// in a single jump.
func MinimumJumps(maxSteps []int) int {
if len(maxSteps) == 1 {
return 0
}
minJumps := int(^uint(0) >> 1)
if maxSteps[0] == 0 { // can't jump from this cell
return minJumps
}
for jump := maxSteps[0]; jump > 0; jump-- {
if jump < len(maxSteps) {
if result := MinimumJumps(maxSteps[jump:]); result < minJumps {
minJumps = result
}
}
}
return minJumps + 1
}
27 changes: 27 additions & 0 deletions day245/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package day245

import "testing"

var testcases = []struct {
maxSteps []int
expectedJumps int
}{
{[]int{6, 2, 4, 0, 5, 1, 1, 4, 2, 9}, 2},
}

func TestMinimumJumps(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if jumps := MinimumJumps(tc.maxSteps); jumps != tc.expectedJumps {
t.Errorf("Expected %v, got %v", tc.expectedJumps, jumps)
}
}
}

func BenchmarkMinimumJumps(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
MinimumJumps(tc.maxSteps)
}
}
}

0 comments on commit 7da382f

Please sign in to comment.