Skip to content

Commit 1c59640

Browse files
committed
Sync LeetCode submission Runtime - 500 ms (19.48%), Memory - 15.4 MB (100.00%)
1 parent 7fc228e commit 1c59640

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

0055-jump-game/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p>
2+
3+
<p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> otherwise</em>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [2,3,1,1,4]
10+
<strong>Output:</strong> true
11+
<strong>Explanation:</strong> Jump 1 step from index 0 to 1, then 3 steps to the last index.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [3,2,1,0,4]
18+
<strong>Output:</strong> false
19+
<strong>Explanation:</strong> You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
27+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
28+
</ul>

0055-jump-game/solution.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def canJump(self, nums: List[int]) -> bool:
3+
max_reach = 0
4+
for i, n in enumerate(nums):
5+
if i > max_reach:
6+
return False
7+
max_reach = max(max_reach, i + n)
8+
9+
return True
10+

0 commit comments

Comments
 (0)