<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jump &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/jump/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 06 Apr 2025 14:51:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>jump &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 403. Frog Jump</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-403-frog-jump/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-403-frog-jump/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Apr 2025 04:11:01 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[jump]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10275</guid>

					<description><![CDATA[肯定是用DP，但我自己想了一个奇怪的方法，勉强过了&#8230; 使用dp[i] = {steps}，表示可以达到stones[i]的最后一跳的unit的集合。dp[0] = {0}dp[1] = {1} if stones[1] = 1 else {}然后对于stones[i]，枚举所有step &#8211; 1, step, step + 1三种情况，看看是否可以跳到新的石头上面（使用一个hashtable判断），如果可以的吧，把跳的unit存到dp[j]中。相当于推了。需要使用hashset去重，不然会超时。 时间复杂度：O(n2)空间复杂度：O(n2) [crayon-67fcefd76cfff384953613/] &#8220;优化&#8221;：由于每次只能k-1,k,k+1steps，所以最大的units和n是线性关系，达到stones[i]最多跳i+1个units。可以用二维数组dp[j][k] := 是否可以通过k步跳到stones[j].dp[j][k]&#8230;]]></description>
										<content:encoded><![CDATA[
<p>肯定是用DP，但我自己想了一个奇怪的方法，勉强过了&#8230;</p>



<p>使用dp[i] = {steps}，表示可以达到stones[i]的最后一跳的unit的集合。<br>dp[0] = {0}<br>dp[1] = {1} if stones[1] = 1 else {}<br>然后对于stones[i]，枚举所有step &#8211; 1, step, step + 1三种情况，看看是否可以跳到新的石头上面（使用一个hashtable判断），如果可以的吧，把跳的unit存到dp[j]中。相当于推了。<br>需要使用hashset去重，不然会超时。</p>



<p>时间复杂度：O(n<sup>2</sup>)<br>空间复杂度：O(n<sup>2</sup>)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  bool canCross(vector&lt;int&gt;&amp; stones) {
    if (stones[1] != 1) return false;
    unordered_map&lt;int, int&gt; m;
    const int n = stones.size();
    for (int i = 0; i &lt; n; ++i)
      m[stones[i]] = i;
    // dp[i] = {steps} steps to jump to stone[i].
    vector&lt;unordered_set&lt;int&gt;&gt; dp(n);
    dp[0] = {0};
    dp[1] = {1};
    for (int i = 2; i &lt; n; ++i) {
      for (int last : dp[i - 1]) {
        for (int cur = last - 1; cur &lt;= last + 1; cur++) {
          if (cur &lt; 1) continue;
          const int t = stones[i - 1] + cur;
          auto it = m.find(t);
          if (it != end(m)) dp[it-&gt;second].insert(cur);
        }
      }
    }
    return !dp.back().empty();
  }
};</pre>



<p>&#8220;优化&#8221;：由于每次只能k-1,k,k+1steps，所以最大的units和n是线性关系，达到stones[i]最多跳i+1个units。<br>可以用二维数组dp[j][k] := 是否可以通过k步跳到stones[j].<br>dp[j][k] = dp[i][k-1] || dp[i][k] || dp[i][k+1], k = stones[j] &#8211; stones[i]. 即从stones[i]跳到stones[j]，并且要求跳到stones[i]的可能步数中存在k-1,k,k+1。<br>时间复杂度和空间复杂度是一样的。</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  bool canCross(vector&lt;int&gt;&amp; stones) {
    const int n = stones.size();
    vector&lt;vector&lt;bool&gt;&gt; dp(n, vector&lt;bool&gt;(n + 1));
    dp[0][0] = true;
    for (int i = 1; i &lt; n; ++i)
      for (int j = 0; j &lt; i; ++j) {
        const int d = stones[i] - stones[j];
        if (d &gt;= n) continue; // undefined range.
        dp[i][d] = dp[i][d] || (dp[j][d - 1] || dp[j][d] || dp[j][d + 1]);
      }
    return any_of(begin(dp.back()), end(dp.back()), [](bool x) { return x; });
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-403-frog-jump/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2770. Maximum Number of Jumps to Reach the Last Index</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2770-maximum-number-of-jumps-to-reach-the-last-index/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2770-maximum-number-of-jumps-to-reach-the-last-index/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 12 Jul 2023 01:27:39 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[jump]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10060</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;array&#160;nums&#160;of&#160;n&#160;integers and an integer&#160;target. You are initially positioned at index&#160;0. In one step, you can jump from index&#160;i&#160;to any index&#160;j&#160;such that: 0&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;array&nbsp;<code>nums</code>&nbsp;of&nbsp;<code>n</code>&nbsp;integers and an integer&nbsp;<code>target</code>.</p>



<p>You are initially positioned at index&nbsp;<code>0</code>. In one step, you can jump from index&nbsp;<code>i</code>&nbsp;to any index&nbsp;<code>j</code>&nbsp;such that:</p>



<ul class="wp-block-list"><li><code>0 &lt;= i &lt; j &lt; n</code></li><li><code>-target &lt;= nums[j] - nums[i] &lt;= target</code></li></ul>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum number of jumps</strong>&nbsp;you can make to reach index</em>&nbsp;<code>n - 1</code>.</p>



<p>If there is no way to reach index&nbsp;<code>n - 1</code>, return&nbsp;<code>-1</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,3,6,4,1,2], target = 2
<strong>Output:</strong> 3
<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:
- Jump from index 0 to index 1. 
- Jump from index 1 to index 3.
- Jump from index 3 to index 5.
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>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,3,6,4,1,2], target = 3
<strong>Output:</strong> 5
<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:
- Jump from index 0 to index 1.
- Jump from index 1 to index 2.
- Jump from index 2 to index 3.
- Jump from index 3 to index 4.
- Jump from index 4 to index 5.
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>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,3,6,4,1,2], target = 0
<strong>Output:</strong> -1
<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. 
</pre>



<p><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>2 &lt;= nums.length == n &lt;= 1000</code></li><li><code>-10<sup>9</sup>&nbsp;&lt;= nums[i]&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>0 &lt;= target &lt;= 2 * 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: DP</strong></h2>



<p>Let dp(i) denotes the maximum jumps from index i to index n-1.</p>



<p>For each index i, try jumping to all possible index j.</p>



<p>dp(i) = max(1 + dp(j)) if j &gt; i and abs(nums[j] &#8211; nums[i) &lt;= target else -1</p>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="urvanov-syntax-highlighter-plain-tag"># Author: Huahua
class Solution:
  def maximumJumps(self, nums: List[int], target: int) -&gt; int:
    n = len(nums)
    @cache
    def dp(i):
      if i == n - 1: return 0
      ans = -1
      for j in range(i + 1, n):
        if abs(nums[j] - nums[i]) &lt;= target and dp(j) != -1:
          ans = max(ans, 1 + dp(j))
      return ans
    
    return dp(0)</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2770-maximum-number-of-jumps-to-reach-the-last-index/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 45. Jump Game II</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-45-jump-game-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-45-jump-game-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Apr 2019 18:44:27 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[jump]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5118</guid>

					<description><![CDATA[Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given an array of non-negative integers, you are initially positioned at the first index of the array.</p>



<p>Each element in the array represents your maximum jump length at that position.</p>



<p>Your goal is to reach the last index in the minimum number of jumps.</p>



<p><strong>Example:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> [2,3,1,1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The minimum number of jumps to reach the last index is 2.
    Jump 1 step from index 0 to 1, then 3 steps to the last index.</pre>



<p><strong>Note:</strong></p>



<p>You can assume that you can always reach the last index.</p>



<h2 class="wp-block-heading"><strong>Solution: Greedy</strong></h2>



<p>Jump as far as possible but lazily. </p>



<pre class="wp-block-preformatted;crayon:false">
[2, 3, 1, 1, 4]
i    nums[i]   steps   near   far
-      -         0       0     0
0      2         0       0     2
1      3         1       2     4
2      1         1       2     4
3      1         2       4     4
4      4         2       4     8
</pre>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua, running time: 12 ms / 10.3 MB
class Solution {
public:
  int jump(vector&lt;int&gt;&amp; nums) {
    int steps = 0;
    int near = 0;
    int far = 0;
    for (int i = 0; i &lt; nums.size(); ++i) {
      if (i &gt; near) {
        ++steps;
        near = far;
      }
      far = max(far, i + nums[i]);      
    }
    return steps;
  }
};</pre>
</div></div>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-45-jump-game-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 55. Jump Game</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-55-jump-game/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-55-jump-game/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 15 Nov 2018 16:37:06 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[jump]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4313</guid>

					<description><![CDATA[Problem Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your&#8230;]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array of non-negative integers, you are initially positioned at the first index of the array.</p>
<p>Each element in the array represents your maximum jump length at that position.</p>
<p>Determine if you are able to reach the last index.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> [2,3,1,1,4]
<strong>Output:</strong> true
<strong>Explanation:</strong> Jump 1 step from index 0 to 1, then 3 steps to the last index.
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> [3,2,1,0,4]
<strong>Output:</strong> false
<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.
</pre>
<h1><strong>Solution: Greedy</strong></h1>
<p>If you can jump to i, then you can jump to at least i + nums[i].</p>
<p>Always jump as far as you can.</p>
<p>Keep tracking the farthest index you can jump to.</p>
<p>Init far = nums[0].</p>
<p>far = max(far, i + nums[i])</p>
<p>check far &gt;= n &#8211; 1</p>
<p>ex 1 [2,3,1,1,4]</p><pre class="urvanov-syntax-highlighter-plain-tag">i    nums[i]   i + nums[i]  far
-      -          -          2
0      2          2          2
1      3          4          4
2      1          3          4
3      1          4          4
4      4          8          8</pre><p>ex 2 [3,2,1,0,4]</p><pre class="urvanov-syntax-highlighter-plain-tag">i    nums[i]   i + nums[i]  far
-      -          -          3
0      3          3          3
1      2          3          3
2      1          3          3
3      0          3          3 &lt;- can not reach index 4, break</pre><p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua, running time 16 ms
class Solution {
public:
  bool canJump(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int far = nums[0];
    for (int i = 0; i &lt; n; i++) {
      if (i &gt; far) break;
      far = max(far, i + nums[i]);
    }
    return far &gt;= n-1;
  }
};</pre><p></div></div></p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-55-jump-game/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
