Created
January 22, 2019 07:51
-
-
Save jovianlin/e416f0f3297b5eced22eefea69f66746 to your computer and use it in GitHub Desktop.
leetcode-jump-game-ii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def jump(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
if not nums or len(nums) == 1: | |
return 0 | |
curr_ladder_start = 0 | |
curr_ladder_steps = nums[0] | |
curr_ladder_end = curr_ladder_start + curr_ladder_steps | |
max_start = curr_ladder_start | |
max_steps = curr_ladder_steps | |
max_end = curr_ladder_end | |
jump = 1 | |
for i in range(1, len(nums)): | |
new_ladder_start = i | |
new_ladder_steps = nums[i] | |
new_ladder_end = new_ladder_start + new_ladder_steps | |
if new_ladder_end > max_end: | |
max_start = new_ladder_start | |
max_steps = new_ladder_steps | |
max_end = new_ladder_end | |
if i == curr_ladder_end and i != len(nums)-1: | |
curr_ladder_start = max_start | |
curr_ladder_steps = max_steps | |
curr_ladder_end = max_end | |
jump += 1 | |
return jump | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment