-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq45_Jump_Game_II.py
More file actions
36 lines (34 loc) · 888 Bytes
/
q45_Jump_Game_II.py
File metadata and controls
36 lines (34 loc) · 888 Bytes
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
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i=0
goal = len(nums)-1
step = 0
while i < goal:
if i + nums[i]>= goal:
return step + 1
else:
beststep = 1
maxsum = 2
for j in range(1,nums[i]+1):
sumstep = j + nums[i+j]
if sumstep > maxsum:
beststep = j
maxsum = sumstep
i+=beststep
step+=1
return step
def jump1(self,nums):
lt=len(nums)-1
mx=right=ct=0
for i in range(lt):
mx=max(i+nums[i],mx)
if i==right:
ct+=1
right=mx
return ct
nums = [2,3,1,1,4]
print(Solution().jump(nums))