Skip to content

Commit f27a86f

Browse files
committed
https://leetcode.com/problems/jump-game/
1 parent ab80f73 commit f27a86f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

MediumProblems/JumpGame.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCode.MediumProblems;
2+
3+
/// <summary>
4+
/// https://leetcode.com/problems/jump-game/
5+
/// </summary>
6+
public class JumpGame
7+
{
8+
public bool CanJump(int[] nums) {
9+
// Tracks the farthest reachable index from current position
10+
int lastReachable = nums.Length - 1;
11+
12+
// Check positions in reverse (from second-last to first)
13+
for (int i = nums.Length - 2; i >= 0; i--) {
14+
// Update last reachable index if current position can reach it
15+
if (i + nums[i] >= lastReachable) {
16+
lastReachable = i;
17+
}
18+
}
19+
20+
// Start index is reachable if we propagated reachability to position 0
21+
return lastReachable == 0;
22+
}
23+
}

0 commit comments

Comments
 (0)