We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ab80f73 commit f27a86fCopy full SHA for f27a86f
MediumProblems/JumpGame.cs
@@ -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