Skip to content

Commit 8334ce9

Browse files
Add files via upload
1 parent d5b3522 commit 8334ce9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Jump Game II/Jump_Game_II.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 60ms 75.10%
2+
class Solution:
3+
def jump(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: int
7+
"""
8+
current_index = 0
9+
step = 0
10+
max_index = len(nums) - 1
11+
12+
while current_index < max_index:
13+
if current_index + nums[current_index] >= max_index:
14+
step += 1
15+
return step
16+
temp_list = nums[current_index + 1:current_index + 1 + nums[current_index]]
17+
temp_list = [num + index for index, num in enumerate(temp_list)]
18+
19+
current_index += (temp_list.index(max(temp_list)) + 1)
20+
step += 1
21+
22+
return step

Jump Game/Jump_Game.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 44ms 97.44%
2+
class Solution:
3+
def canJump(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: bool
7+
"""
8+
current_index, len_nums = 0, len(nums)
9+
if 0 not in nums:
10+
return True
11+
if nums[0] is 0:
12+
return False if len_nums > 1 else True
13+
14+
while True:
15+
value_list = [index + num for index, num in enumerate(nums[current_index + 1:current_index + nums[current_index] + 1])]
16+
max_step = value_list.index(max(value_list)) + 1
17+
18+
if current_index + max_step >= len_nums - 1:
19+
return True
20+
21+
current_index += max_step
22+
23+
if nums[current_index] is 0:
24+
return False

0 commit comments

Comments
 (0)