<p>Your car starts at position <code>0</code> and speed <code>+1</code> on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions <code>&#39;A&#39;</code> (accelerate) and <code>&#39;R&#39;</code> (reverse):</p>

<ul>
	<li>When you get an instruction <code>&#39;A&#39;</code>, your car does the following:

	<ul>
		<li><code>position += speed</code></li>
		<li><code>speed *= 2</code></li>
	</ul>
	</li>
	<li>When you get an instruction <code>&#39;R&#39;</code>, your car does the following:
	<ul>
		<li>If your speed is positive then <code>speed = -1</code></li>
		<li>otherwise <code>speed = 1</code></li>
	</ul>
	Your position stays the same.</li>
</ul>

<p>For example, after commands <code>&quot;AAR&quot;</code>, your car goes to positions <code>0 --&gt; 1 --&gt; 3 --&gt; 3</code>, and your speed goes to <code>1 --&gt; 2 --&gt; 4 --&gt; -1</code>.</p>

<p>Given a target position <code>target</code>, return <em>the length of the shortest sequence of instructions to get there</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> target = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The shortest instruction sequence is &quot;AA&quot;.
Your position goes from 0 --&gt; 1 --&gt; 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> target = 6
<strong>Output:</strong> 5
<strong>Explanation:</strong> 
The shortest instruction sequence is &quot;AAARA&quot;.
Your position goes from 0 --&gt; 1 --&gt; 3 --&gt; 7 --&gt; 7 --&gt; 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= target &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int racecar(int target) {
        int count = 0;

        queue<pair<int, int>> q;
        // {speed pos}

        q.push({1,0});

        unordered_map<long, unordered_map<long,int>> mp;

        while(!q.empty())
        {
            count ++;

            int sz = q.size();
            while(sz--)
            {
                auto it = q.front(); q.pop();
                long speed = it.first;
                long pos = it.second;

                // a wala case
                long pos1 = pos + speed;
                long speed1 = speed*2;

                if(pos1 == target) return count;
                if(mp.count(speed1) && mp[speed1].count(pos1)) {
                    continue;
                }
                else {
                    q.push({speed1, pos1});
                    mp[speed1][pos1] = 1;
                }


                // r wala case
                long pos2 = pos;
                long speed2 = speed > 0 ? -1 : 1;

                if(pos2 == target) return count;

                if(pos > 0) 
                { 
                    if(mp.count(speed2) && mp[speed2].count(pos2)) 
                    {
                        continue;
                    }
                    else {
                        q.push({speed2, pos2});
                        mp[speed2][pos2] = 1;
                    }
                }

            }
        }

        return 0;
    }
};
186285