forked from alqamahjsr/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55_Jump_Game.py
77 lines (73 loc) · 2.77 KB
/
55_Jump_Game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Solution(object):
# # Backtracking Approach - TLE
# def canJump(self, nums):
# """
# :type nums: List[int]
# :rtype: bool
# """
# return self.canJumpHelper(nums, 0)
#
# def canJumpHelper(self, nums, currentIdx):
# if currentIdx == len(nums) - 1:
# return True
# nextMaxJumpIdx = min(currentIdx + nums[currentIdx], len(nums) - 1)
# for i in range(nextMaxJumpIdx, currentIdx,
# -1): # Python for loop decrementing index >> https://stackoverflow.com/questions/28650128/python-for-loop-decrementing-index/28650284
# if self.canJumpHelper(nums, i):
# return True
# return False
#
# # DP top-down with memoization Approach - TLE
# def canJump(self, nums):
# """
# :type nums: List[int]
# :rtype: bool
# """
# memo = [0] * len(nums)
# memo[-1] = True # Here True means reachable and True means not rechable
# return self.canJumpHelper(nums, 0, memo)
#
# def canJumpHelper(self, nums, currentIdx, memo):
# if memo[currentIdx] != 0:
# return memo[currentIdx]
# nextMaxJumpIdx = min(currentIdx + nums[currentIdx], len(nums) - 1)
# for i in range(nextMaxJumpIdx, currentIdx, -1):
# if self.canJumpHelper(nums, i, memo):
# memo[currentIdx] = True
# return True
# memo[currentIdx] = False
# return False
#
# # DP bottom-up with memoization Approach - TLE
# def canJump(self, nums):
# """
# :type nums: List[int]
# :rtype: bool
# """
# memo = [0] * len(nums)
# memo[-1] = True # Here True means reachable and True means not rechable
# for i in range(len(nums) - 1, -1, -1):
# nextMaxJumpIdx = min(i + nums[i], len(nums) - 1)
# for j in range(i + 1, nextMaxJumpIdx + 1):
# if memo[j] is True:
# memo[i] = True
# break
# return True if memo[0] == True else False
# Greedy
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
lastPosition = len(nums) - 1
for i in range(len(nums) - 1, -1, -1):
nextMaxJump = i + nums[i]
if nextMaxJump >= lastPosition:
lastPosition = i
return True if lastPosition == 0 else False
sol = Solution()
# input = [2,3,0,1,4]
input = [3,2,1,0,4]
# input = [2,0,6,9,8,4,5,0,8,9,1,2,9,6,8,8,0,6,3,1,2,2,1,2,6,5,3,1,2,2,6,4,2,4,3,0,0,0,3,8,2,4,0,1,2,0,1,4,6,5,8,0,7,9,3,4,6,6,5,8,9,3,4,3,7,0,4,9,0,9,8,4,3,0,7,7,1,9,1,9,4,9,0,1,9,5,7,7,1,5,8,2,8,2,6,8,2,2,7,5,1,7,9,6]
output = sol.canJump(input)
print('res: ', output)