Skip to content

Commit 97d6228

Browse files
add 55 alt sol
1 parent dac7287 commit 97d6228

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.contest.dp
2+
3+
/**
4+
* 55. Jump Game
5+
* Alternative Solution
6+
* Recursion Approach
7+
*/
8+
9+
fun canJumpAltSolution(nums: IntArray): Boolean = solve(nums, 0)
10+
11+
private fun solve(nums: IntArray, index: Int): Boolean {
12+
if (index >= nums.size - 1) return true
13+
if (nums[index] == 0 && index < nums.size - 1) return false
14+
15+
var step = nums[index]
16+
17+
while (step != 0) {
18+
if (solve(nums, index + step)) return true
19+
step--
20+
}
21+
22+
return false
23+
}

0 commit comments

Comments
 (0)