-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpGameII.py
More file actions
36 lines (34 loc) · 1.29 KB
/
Copy pathjumpGameII.py
File metadata and controls
36 lines (34 loc) · 1.29 KB
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:
# Initial approach: Greedy algorithm that selects the next jump by checking all possible positions
# within the current jump range and choosing the one that allows the farthest reach.
# def jump(self, nums: List[int]) -> int:
# cost = 0
# i = 0
# while i < len(nums) - 1:
# maxJump = 0
# index = 0
# j = 1
# while i+j < len(nums) and j <= nums[i]:
# if maxJump < nums[i+j] + i + j:
# maxJump = nums[i+j] + i + j
# index = i+j
# j += 1
# if i+j == len(nums):
# return cost + 1
# cost += 1
# i = index
# return cost
# Optimized approach: Optimized greedy algorithm that tracks the current jump boundary (`end`) and
# the farthest reachable position (`farther`). Minimizes jumps by expanding the boundary only when necessary.
def jump(self, nums: List[int]) -> int:
cost = 0
end = 0
farther = 0
for i in range(len(nums) - 1):
farther = max(farther, nums[i] + i)
if farther >= len(nums) - 1:
return cost + 1
if i == end:
cost += 1
end = farther
return cost