Skip to content

Commit 0e2133c

Browse files
Sync LeetCode submission Runtime - 11 ms (93.52%), Memory - 68.6 MB (41.05%)
1 parent 703dabf commit 0e2133c

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers and an integer <code>target</code>.</p>
2+
3+
<p>You are initially positioned at index <code>0</code>. In one step, you can jump from index <code>i</code> to any index <code>j</code> such that:</p>
4+
5+
<ul>
6+
<li><code>0 &lt;= i &lt; j &lt; n</code></li>
7+
<li><code>-target &lt;= nums[j] - nums[i] &lt;= target</code></li>
8+
</ul>
9+
10+
<p>Return <em>the <strong>maximum number of jumps</strong> you can make to reach index</em> <code>n - 1</code>.</p>
11+
12+
<p>If there is no way to reach index <code>n - 1</code>, return <code>-1</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 2
19+
<strong>Output:</strong> 3
20+
<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
21+
- Jump from index 0 to index 1.
22+
- Jump from index 1 to index 3.
23+
- Jump from index 3 to index 5.
24+
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. </pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 3
30+
<strong>Output:</strong> 5
31+
<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
32+
- Jump from index 0 to index 1.
33+
- Jump from index 1 to index 2.
34+
- Jump from index 2 to index 3.
35+
- Jump from index 3 to index 4.
36+
- Jump from index 4 to index 5.
37+
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5. </pre>
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 0
43+
<strong>Output:</strong> -1
44+
<strong>Explanation:</strong> It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>2 &lt;= nums.length == n &lt;= 1000</code></li>
52+
<li><code>-10<sup>9</sup>&nbsp;&lt;= nums[i]&nbsp;&lt;= 10<sup>9</sup></code></li>
53+
<li><code>0 &lt;= target &lt;= 2 * 10<sup>9</sup></code></li>
54+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maximumJumps(vector<int>& nums, int target) {
4+
int n = nums.size();
5+
vector<int> dp(n, -1e9); dp[0] = 0;
6+
for (int i=1; i<n; i++) {
7+
for (int j=0; j<i; j++) {
8+
if (abs(nums[i] - nums[j]) <= target) dp[i] = max(dp[i], dp[j] + 1);
9+
}
10+
}
11+
return dp[n-1] > 0 ? dp[n-1] : -1;
12+
}
13+
};

0 commit comments

Comments
 (0)