Skip to content

Commit 8c942d0

Browse files
committed
leetcode 55
python
1 parent 1123ca5 commit 8c942d0

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
5151
| 49 | Medium | [Group Anagrams](leetcode/49-Medium-Group-Anagrams/problem.md) | [Python](leetcode/49-Medium-Group-Anagrams/answer.py) |
5252
| 53 | Easy | [Maximum Subarray](leetcode/53-Easy-Maximum-Subarray/problem.md) | [Java](leetcode/53-Easy-Maximum-Subarray/answer.java) |
5353
| 54 | Medium | [Spiral Matrix](leetcode/54-Medium-Spiral-Matrix/problem.md) | [Python](leetcode/54-Medium-Spiral-Matrix/answer.py) |
54+
| 55 | Medium | [Jump Game](leetcode/55-Medium-Jump-Game/problem.md) | [Python](leetcode/55-Medium-Jump-Game/answer.py) |
5455
| 56 | Medium | [Merge Intervals](leetcode/56-Medium-Merge-Intervals/problem.md) | [Python](leetcode/56-Medium-Merge-Intervals/answer.py) |
5556
| 58 | Easy | [Length of Last Word](leetcode/58-Easy-Length-Of-Last-Word/problem.md) | [Python](leetcode/58-Easy-Length-Of-Last-Word/answer.py) |
5657
| 62 | Medium | [Unique Paths](leetcode/62-Medium-Unique-Paths/problem.md) | [Python](leetcode/62-Medium-Unique-Paths/answer.py) |
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
# Brute Force O(n^2)
5+
#------------------------------------------------------------------------------
6+
7+
class Solution(object):
8+
def canJump(self, nums):
9+
"""
10+
:type nums: List[int]
11+
:rtype: bool
12+
"""
13+
if len(nums) <= 1:
14+
return True
15+
valid = [len(nums)-1]
16+
done = []
17+
while valid:
18+
curr = valid.pop(0)
19+
if curr not in done:
20+
done.append(curr)
21+
valid += self.getList(nums, curr)
22+
if 0 in valid:
23+
return True
24+
return False
25+
26+
def getList(self, nums, idx):
27+
result = []
28+
for i in range(0, idx):
29+
if nums[i] >= (idx - i):
30+
result.append(i)
31+
return result
32+
33+
#------------------------------------------------------------------------------
34+
# Optimized O(n)
35+
#------------------------------------------------------------------------------
36+
37+
class Solution(object):
38+
def canJump(self, nums):
39+
if not nums:
40+
return False
41+
curr = len(nums) - 1
42+
for i in range(len(nums)-1, -1, -1):
43+
if nums[i] >= curr - i:
44+
curr = i
45+
return True if not curr else False
46+
47+
48+
#------------------------------------------------------------------------------
49+
#Testing
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Problem 55: Jump Game
2+
3+
#### Difficulty: Medium
4+
5+
#### Problem
6+
7+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
8+
9+
Each element in the array represents your maximum jump length at that position.
10+
11+
Determine if you are able to reach the last index.
12+
13+
#### Example
14+
15+
<pre>
16+
Input: [2,3,1,1,4]
17+
Output: true
18+
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
19+
20+
Input: [3,2,1,0,4]
21+
Output: false
22+
Explanation: You will always arrive at index 3 no matter what. Its maximum
23+
jump length is 0, which makes it impossible to reach the last index.
24+
</pre>

0 commit comments

Comments
 (0)