<?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 game Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/jump-game/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/jump-game/</link>
	<description></description>
	<lastBuildDate>Sat, 08 Feb 2020 17:34:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>jump game Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/jump-game/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1345. Jump Game IV</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-1345-jump-game-iv/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-1345-jump-game-iv/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 08 Feb 2020 17:29:47 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[jump game]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6269</guid>

					<description><![CDATA[<p>Given an array of&#160;integers&#160;arr, you are initially positioned at the first index of the array. In one step you can jump from index&#160;i&#160;to index: i&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1345-jump-game-iv/">花花酱 LeetCode 1345. Jump Game IV</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an array of&nbsp;integers&nbsp;<code>arr</code>, you are initially positioned at the first index of the array.</p>



<p>In one step you can jump from index&nbsp;<code>i</code>&nbsp;to index:</p>



<ul><li><code>i + 1</code>&nbsp;where:&nbsp;<code>i + 1 &lt; arr.length</code>.</li><li><code>i - 1</code>&nbsp;where:&nbsp;<code>i - 1 &gt;= 0</code>.</li><li><code>j</code>&nbsp;where:&nbsp;<code>arr[i] == arr[j]</code>&nbsp;and&nbsp;<code>i != j</code>.</li></ul>



<p>Return&nbsp;<em>the minimum number of steps</em>&nbsp;to reach the&nbsp;<strong>last index</strong>&nbsp;of the array.</p>



<p>Notice that you can not jump outside of the array at any time.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [100,-23,-23,404,100,23,23,23,3,404]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You need three jumps from index 0 --&gt; 4 --&gt; 3 --&gt; 9. Note that index 9 is the last index of the array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [7]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Start index is the last index. You don't need to jump.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [7,6,9,6,9,6,9,7]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can jump directly from index 0 to index 7 which is last index of the array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [6,1,9]
<strong>Output:</strong> 2
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [11,22,7,7,7,7,7,7,7,22,13]
<strong>Output:</strong> 3
</pre>



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



<ul><li><code>1 &lt;= arr.length &lt;= 5 * 10^4</code></li><li><code>-10^8 &lt;= arr[i] &lt;= 10^8</code></li></ul>



<h2><strong>Solution: HashTable + BFS</strong></h2>



<p>Use a hashtable to store the indices of each unique number.</p>



<p>each index i has neighbors (i-1, i + 1, hashtable[arr[i]])</p>



<p>Use BFS to find the shortest path in this unweighted graph.</p>



<p>Key optimization, clear hashtable[arr[i]] after the first use, since all nodes are already on queue, no longer needed.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua, 124 ms, 30.5 MB
class Solution {
public:
  int minJumps(vector&lt;int&gt;&amp; arr) {
    const int n = arr.size();
    unordered_map&lt;int, vector&lt;int&gt;&gt; m;
    for (int i = 0; i &lt; n; ++i)
      m[arr[i]].push_back(i);
    vector&lt;int&gt; seen(n);
    queue&lt;int&gt; q({0});    
    seen[0] = 1;
    int steps = 0;
    while (!q.empty()) {
      int size = q.size();
      while (size--) {
        int i = q.front(); q.pop();        
        if (i == n - 1) return steps;
        if (i - 1 &gt;= 0 &amp;&amp; !seen[i - 1]++) q.push(i - 1);
        if (i + 1 &lt; n &amp;&amp; !seen[i + 1]++) q.push(i + 1);
        auto it = m.find(arr[i]);
        if (it == m.end()) continue;
        for (int nxt : it-&gt;second)
          if (!seen[nxt]++) q.push(nxt);
        m.erase(it); // no longer needed.
      }
      ++steps;
    }
    return -1;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/greedy/leetcode-55-jump-game/">https://zxi.mytechroad.com/blog/greedy/leetcode-55-jump-game/</a></li><li><a href="https://zxi.mytechroad.com/blog/greedy/leetcode-45-jump-game-ii/">https://zxi.mytechroad.com/blog/greedy/leetcode-45-jump-game-ii/</a></li><li><a href="https://zxi.mytechroad.com/blog/searching/leetcode-1306-jump-game-iii/">https://zxi.mytechroad.com/blog/searching/leetcode-1306-jump-game-iii/</a></li><li><a href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1344-jump-game-v/">https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1344-jump-game-v/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1345-jump-game-iv/">花花酱 LeetCode 1345. Jump Game IV</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/leetcode-1345-jump-game-iv/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
