Skip to content

Commit 182ed6d

Browse files
committed
Add problem 55 - Jump Game
1 parent 58a4199 commit 182ed6d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

problems/greedy/jump_game.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package greedy
2+
3+
func JumpGame(nums []int) bool {
4+
// Current jump
5+
jump := nums[0]
6+
// Process through the remaining array
7+
for i := 1; i < len(nums); i++ {
8+
if jump < i {
9+
return false
10+
}
11+
jump = max(jump, i+nums[i])
12+
}
13+
return true
14+
}

tests/greedy/jump_game_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package greedy_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/anirudhology/leetcode-go/problems/greedy"
7+
)
8+
9+
func TestCanJump(t *testing.T) {
10+
if !greedy.JumpGame([]int{0}) {
11+
t.Error("Expected true for single element array")
12+
}
13+
14+
if greedy.JumpGame([]int{1, 0, 0, 0}) {
15+
t.Error("Expected false for all zeros except first")
16+
}
17+
18+
if !greedy.JumpGame([]int{2, 3, 1, 1, 4}) {
19+
t.Error("Expected true for sufficient jumps")
20+
}
21+
22+
if greedy.JumpGame([]int{3, 2, 1, 0, 4}) {
23+
t.Error("Expected false for insufficient jumps")
24+
}
25+
}

0 commit comments

Comments
 (0)