File tree 3 files changed +74
-0
lines changed
leetcode/55-Medium-Jump-Game
3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
51
51
| 49 | Medium | [ Group Anagrams] ( leetcode/49-Medium-Group-Anagrams/problem.md ) | [ Python] ( leetcode/49-Medium-Group-Anagrams/answer.py ) |
52
52
| 53 | Easy | [ Maximum Subarray] ( leetcode/53-Easy-Maximum-Subarray/problem.md ) | [ Java] ( leetcode/53-Easy-Maximum-Subarray/answer.java ) |
53
53
| 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 ) |
54
55
| 56 | Medium | [ Merge Intervals] ( leetcode/56-Medium-Merge-Intervals/problem.md ) | [ Python] ( leetcode/56-Medium-Merge-Intervals/answer.py ) |
55
56
| 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 ) |
56
57
| 62 | Medium | [ Unique Paths] ( leetcode/62-Medium-Unique-Paths/problem.md ) | [ Python] ( leetcode/62-Medium-Unique-Paths/answer.py ) |
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments