<?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>steps Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/steps/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/steps/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2020 19:49:25 +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>steps Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/steps/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1654. Minimum Jumps to Reach Home</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-1654-minimum-jumps-to-reach-home/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-1654-minimum-jumps-to-reach-home/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Nov 2020 19:46:45 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[steps]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7652</guid>

					<description><![CDATA[<p>A certain bug&#8217;s home is on the x-axis at position&#160;x. Help them get there from position&#160;0. The bug jumps according to the following rules: It&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1654-minimum-jumps-to-reach-home/">花花酱 LeetCode 1654. Minimum Jumps to Reach Home</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>A certain bug&#8217;s home is on the x-axis at position&nbsp;<code>x</code>. Help them get there from position&nbsp;<code>0</code>.</p>



<p>The bug jumps according to the following rules:</p>



<ul><li>It can jump exactly&nbsp;<code>a</code>&nbsp;positions&nbsp;<strong>forward</strong>&nbsp;(to the right).</li><li>It can jump exactly&nbsp;<code>b</code>&nbsp;positions&nbsp;<strong>backward</strong>&nbsp;(to the left).</li><li>It cannot jump backward twice in a row.</li><li>It cannot jump to any&nbsp;<code>forbidden</code>&nbsp;positions.</li></ul>



<p>The bug may jump forward&nbsp;<strong>beyond</strong>&nbsp;its home, but it&nbsp;<strong>cannot jump</strong>&nbsp;to positions numbered with&nbsp;<strong>negative</strong>&nbsp;integers.</p>



<p>Given an array of integers&nbsp;<code>forbidden</code>, where&nbsp;<code>forbidden[i]</code>&nbsp;means that the bug cannot jump to the position&nbsp;<code>forbidden[i]</code>, and integers&nbsp;<code>a</code>,&nbsp;<code>b</code>, and&nbsp;<code>x</code>, return&nbsp;<em>the minimum number of jumps needed for the bug to reach its home</em>. If there is no possible sequence of jumps that lands the bug on position&nbsp;<code>x</code>, return&nbsp;<code>-1.</code></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 jumps forward (0 -&gt; 3 -&gt; 6 -&gt; 9) will get the bug home.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
<strong>Output:</strong> -1
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
<strong>Output:</strong> 2
<strong>Explanation:</strong> One jump forward (0 -&gt; 16) then one jump backward (16 -&gt; 7) will get the bug home.
</pre>



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



<ul><li><code>1 &lt;= forbidden.length &lt;= 1000</code></li><li><code>1 &lt;= a, b, forbidden[i] &lt;= 2000</code></li><li><code>0 &lt;= x &lt;= 2000</code></li><li>All the elements in&nbsp;<code>forbidden</code>&nbsp;are distinct.</li><li>Position&nbsp;<code>x</code>&nbsp;is not forbidden.</li></ul>



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



<p>Normal BFS with two tricks:<br>1. For each position, we need to track whether it&#8217;s reached via a forward jump or backward jump<br>2. How far should we go? If we don&#8217;t limit, it can go forever which leads to TLE/MLE. We can limit the distance to 2*max_jump, e.g. 4000, that&#8217;s maximum distance we can jump back to home in one shot.</p>



<p>Time complexity: O(max_distance * 2)<br>Space complexity: O(max_distance * 2)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumJumps(vector&lt;int&gt;&amp; forbidden, int a, int b, int x) {
    constexpr int kMaxPosition = 4000;
    if (x == 0) return 0;
    queue&lt;pair&lt;int, bool&gt;&gt; q{{{0, true}}};
    unordered_set&lt;int&gt; seen1, seen2;
    for (int f : forbidden) seen1.insert(f), seen2.insert(f);
    seen1.insert(0);
    int steps = 0;
    while (!q.empty()) {      
      int size = q.size();
      while (size--) {
        auto [cur, forward] = q.front(); q.pop();        
        if (cur == x) return steps;
        if (cur &gt; kMaxPosition) continue; // no way to go back
        if (seen1.insert(cur + a).second)      
          q.emplace(cur + a, true);
        if (cur - b &gt;= 0 &amp;&amp; forward &amp;&amp; seen2.insert(cur - b).second)
          q.emplace(cur - b, false);
      }
      ++steps;
    }
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1654-minimum-jumps-to-reach-home/">花花酱 LeetCode 1654. Minimum Jumps to Reach Home</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-1654-minimum-jumps-to-reach-home/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
