You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0000-0099/0055.Jump Game/README_EN.md
+35-10
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,16 @@
35
35
36
36
## Solutions
37
37
38
+
**Approach 1: Greedy**
39
+
40
+
We use a variable $mx$ to maintain the farthest index that can be reached, initially $mx = 0$.
41
+
42
+
We traverse the array from left to right, for each position $i$ we are currently traversing, if $mx \lt i$, it means that the current position cannot be reached, directly return `false`. Otherwise, the farthest position that can be reached from position $i$ by jumping is $i+nums[i]$, we use $i+nums[i]$ to update the value of $mx$, that is $mx = \max(mx, i + nums[i])$.
43
+
44
+
When the traversal ends, return `true` directly.
45
+
46
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
0 commit comments