Thanks to visit codestin.com Credit goes to github.com
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 3f2556b commit 5c2d997Copy full SHA for 5c2d997
jump_game_ii/solution2.py
@@ -12,13 +12,28 @@ def jump(self, A):
12
min_i = n - 1 # Mininum index able to reach `cur`
13
cur = n - 1 # Current index to reach (in a loop)
14
i = cur - 1
15
- # O(n^2) time
+ reached = False # Whether `cur` can be reached by previous elements
16
while i >= 0:
17
18
if t[i] >= cur:
19
min_i = i
20
+ reached = True
21
i -= 1
22
+ if not reached:
23
+ return -1
24
+ reached = False
25
count += 1
26
cur = min_i
27
28
return count
29
+
30
31
+a1 = [2, 3, 1, 1, 4]
32
+a2 = [3, 2, 1, 0, 4]
33
+a3 = [1, 1, 1, 1, 1, 1]
34
+a4 = [1, 0, 1, 1, 1, 1]
35
+s = Solution()
36
+print s.jump(a1)
37
+print s.jump(a2)
38
+print s.jump(a3)
39
+print s.jump(a4)
0 commit comments