<p>You are given an integer array <code>arr</code>. From some starting index, you can make a series of jumps. The (1<sup>st</sup>, 3<sup>rd</sup>, 5<sup>th</sup>, ...) jumps in the series are called <strong>odd-numbered jumps</strong>, and the (2<sup>nd</sup>, 4<sup>th</sup>, 6<sup>th</sup>, ...) jumps in the series are called <strong>even-numbered jumps</strong>. Note that the <strong>jumps</strong> are numbered, not the indices.</p>

<p>You may jump forward from index <code>i</code> to index <code>j</code> (with <code>i &lt; j</code>) in the following way:</p>

<ul>
	<li>During <strong>odd-numbered jumps</strong> (i.e., jumps 1, 3, 5, ...), you jump to the index <code>j</code> such that <code>arr[i] &lt;= arr[j]</code> and <code>arr[j]</code> is the smallest possible value. If there are multiple such indices <code>j</code>, you can only jump to the <strong>smallest</strong> such index <code>j</code>.</li>
	<li>During <strong>even-numbered jumps</strong> (i.e., jumps 2, 4, 6, ...), you jump to the index <code>j</code> such that <code>arr[i] &gt;= arr[j]</code> and <code>arr[j]</code> is the largest possible value. If there are multiple such indices <code>j</code>, you can only jump to the <strong>smallest</strong> such index <code>j</code>.</li>
	<li>It may be the case that for some index <code>i</code>, there are no legal jumps.</li>
</ul>

<p>A starting index is <strong>good</strong> if, starting from that index, you can reach the end of the array (index <code>arr.length - 1</code>) by jumping some number of times (possibly 0 or more than once).</p>

<p>Return <em>the number of <strong>good</strong> starting indices</em>.</p>

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

<pre>
<strong>Input:</strong> arr = [10,13,12,14,15]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
From starting index i = 0, we can make our 1st jump to i = 2 (since arr[2] is the smallest among arr[1], arr[2], arr[3], arr[4] that is greater or equal to arr[0]), then we cannot jump any more.
From starting index i = 1 and i = 2, we can make our 1st jump to i = 3, then we cannot jump any more.
From starting index i = 3, we can make our 1st jump to i = 4, so we have reached the end.
From starting index i = 4, we have reached the end already.
In total, there are 2 different starting indices i = 3 and i = 4, where we can reach the end with some number of
jumps.
</pre>

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

<pre>
<strong>Input:</strong> arr = [2,3,1,1,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3
During our 3rd jump (odd-numbered), we jump from i = 2 to i = 3 because arr[3] is the smallest value in [arr[3], arr[4]] that is greater than or equal to arr[2].
We can&#39;t jump from i = 3 to i = 4, so the starting index i = 0 is not good.
In a similar manner, we can deduce that:
From starting index i = 1, we jump to i = 4, so we reach the end.
From starting index i = 2, we jump to i = 3, and then we can&#39;t jump anymore.
From starting index i = 3, we jump to i = 4, so we reach the end.
From starting index i = 4, we are already at the end.
In total, there are 3 different starting indices i = 1, i = 3, and i = 4, where we can reach the end with some
number of jumps.
</pre>

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

<pre>
<strong>Input:</strong> arr = [5,1,3,4,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can reach the end from starting indices 1, 2, and 4.
</pre>

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

<ul>
	<li><code>1 &lt;= arr.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= arr[i] &lt; 10<sup>5</sup></code></li>
</ul>

class Solution {
    
    enum class JumpType
    {
        Odd,
        Even
    };
    
public:
    int oddEvenJumps(vector<int>& arr) {
        
        preprocess(arr);
        std::vector<std::map<JumpType, bool>> isGoodIndex(arr.size());
        
        int goodIndexCount = 1;
        
        isGoodIndex[arr.size()-1][JumpType::Odd] = isGoodIndex[arr.size()-1][JumpType::Even] = true;
        
        for(int i= arr.size()-2; i>=0; i--)
        {
            isGoodIndex[i][JumpType::Odd] = m_oddJumps[i]!= i? isGoodIndex[m_oddJumps[i]][JumpType::Even]: 0;
            isGoodIndex[i][JumpType::Even] = m_evenJumps[i]!= i? isGoodIndex[m_evenJumps[i]][JumpType::Odd] : 0;
            
            
            if(isGoodIndex[i][JumpType::Odd])
            {
                goodIndexCount++;
            }
            
        }
        
        return goodIndexCount;
        
    }
    
private:
    
    void preprocess(const std::vector<int>& arr)
    {
        for(int i=0; i< arr.size(); i++)
        {
            m_arr.push_back(std::make_pair(arr[i],i));
        }
        
        std::sort(m_arr.begin(), m_arr.end());
        prepareOddJumps(arr);
        prepareEvenJumps(arr);
    }
    
    void prepareOddJumps(const std::vector<int>& arr)
    {
        m_oddJumps = std::vector<int>(m_arr.size());
        std::stack<int> m_maxs{};
        
        for(int i=m_arr.size()-1; i>= 0; i--)
        {
            int index = m_arr[i].second;
            
            while(!m_maxs.empty() && m_maxs.top() < index)
            {
                int temp = m_maxs.top();
                m_maxs.pop();
                if(arr[temp] == arr[index])
                {
                    m_maxs.push(index);
                    index = temp;
                }
                
            }
            
            if(m_maxs.empty())
            {
                m_oddJumps[index] = index; //no larger element than i occurs after this element 
            }
            else
            {
                m_oddJumps[index] = m_maxs.top(); //otherwise, the smallest of those is at the top
            }
            m_maxs.push(index);
            //cout << "odd:" << index << " " << m_oddJumps[index] << endl;
        }
    }
    
    void prepareEvenJumps(const std::vector<int>& arr)
    {
        m_evenJumps = std::vector<int>(m_arr.size());
        std::stack<int> m_mins{};
        
        std::sort(m_arr.begin(), m_arr.end(), [](std::pair<int,int>& a, std::pair<int,int>&b)
                  {
                      bool smaller = a.first < b.first;
                      bool appearsAfter = a.first == b.first && a.second > b.second;
                      return smaller || appearsAfter;
                  });
        
        for(int i=0; i< m_arr.size(); i++)
        {
            int index = m_arr[i].second;
            
            int next = index;
            while(!m_mins.empty() && m_mins.top() < index)
            {
                m_mins.pop();
            }
            
            if(m_mins.empty())
            {
                m_evenJumps[index] = index; //no element smaller than i occurs after this element
            }
            else
            {
                m_evenJumps[index] = m_mins.top(); //otherwise, the largest such element will be at the top
            }
          //   cout << "even:" << index << " " << m_evenJumps[index] << endl;
            m_mins.push(index);
        }
    }
    
private:
    std::vector<std::pair<int,int>> m_arr{};
    std::vector<int> m_oddJumps{};
    std::vector<int> m_evenJumps{};
};
49213