File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed
Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change 1616Explanation: You will always arrive at index 3 no matter what. Its maximum
1717 jump length is 0, which makes it impossible to reach the last index.
1818"""
19- class Solution3 :
20- # Time Complexity: O(n)
21- # Space Complexity: O(1)
19+ class Solution :
20+ def canJump (self , nums : List [int ]) -> bool :
21+ max_reach = nums [0 ]
22+
23+ for index in range (1 , len (nums )):
24+ if index > max_reach :
25+ break
26+ max_reach = max (max_reach , index + nums [index ])
27+
28+ return max_reach >= len (nums ) - 1
29+
30+
31+ class Solution1 :
2232 def canJump (self , nums : List [int ]) -> bool :
2333 lastPos = len (nums ) - 1
2434 for i in range (len (nums )- 1 , - 1 , - 1 ):
2535 if i + nums [i ] >= lastPos :
2636 lastPos = i
27- return lastPos == 0
37+ return lastPos == 0
You can’t perform that action at this time.
0 commit comments