<p>Alice and Bob play a game with piles of stones. There are an <strong>even</strong> number of piles arranged in a row, and each pile has a <strong>positive</strong> integer number of stones <code>piles[i]</code>.</p>

<p>The objective of the game is to end with the most stones. The <strong>total</strong> number of stones across all the piles is <strong>odd</strong>, so there are no ties.</p>

<p>Alice and Bob take turns, with <strong>Alice starting first</strong>. Each turn, a player takes the entire pile of stones either from the <strong>beginning</strong> or from the <strong>end</strong> of the row. This continues until there are no more piles left, at which point the person with the <strong>most stones wins</strong>.</p>

<p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins the game, or </em><code>false</code><em> if Bob wins</em>.</p>

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

<pre>
<strong>Input:</strong> piles = [5,3,4,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> 
Alice starts first, and can only take the first 5 or the last 5.
Say she takes the first 5, so that the row becomes [3, 4, 5].
If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points.
If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
</pre>

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

<pre>
<strong>Input:</strong> piles = [3,7,2,3]
<strong>Output:</strong> true
</pre>

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

<ul>
	<li><code>2 &lt;= piles.length &lt;= 500</code></li>
	<li><code>piles.length</code> is <strong>even</strong>.</li>
	<li><code>1 &lt;= piles[i] &lt;= 500</code></li>
	<li><code>sum(piles[i])</code> is <strong>odd</strong>.</li>
</ul>

class Solution {
public:
    // Define a hash function for std::pair<int, int>
    struct pair_hash {
        template <class T1, class T2>
        std::size_t operator() (const std::pair<T1, T2>& pair) const {
            return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
        }
    };

    // Define an equality function for std::pair<int, int>
    struct pair_equal {
        bool operator() (const std::pair<int, int>& lhs, const std::pair<int, int>& rhs) const {
            return lhs.first == rhs.first && lhs.second == rhs.second;
        }
    };


    bool stoneGame(vector<int>& piles) {
        std::unordered_map<std::pair<int, int>, int, pair_hash, pair_equal> best_for_subarray = {}; // <l, r> -> alice_advantage
        best_for_subarray[{0, piles.size() - 1}] = 0;
        
        queue<pair<int, int>> q = {};
        q.push({0, piles.size() - 1});

        while (!q.empty()) {
            pair<int, int> lr = q.front();
            q.pop();

            int l = lr.first;
            int r = lr.second;

            int best_this_subarray = best_for_subarray[{l, r}];
            if (l >= r) {
                if (best_this_subarray > 0) return true;
                else continue;
            }

            int candidate;

            // alice left bob right
            candidate = best_this_subarray + piles[l] - piles[r];
            if (best_for_subarray.contains({l + 1, r - 1})) {
                best_for_subarray[{l + 1, r - 1}] = max(best_for_subarray[{l + 1, r - 1}], candidate);
            } else {
                best_for_subarray[{l + 1, r - 1}] = candidate;
                q.push({l + 1, r - 1});
            }

            // alice right bob left
            candidate = best_this_subarray + piles[r] - piles[l];
            best_for_subarray[{l + 1, r - 1}] = max(best_for_subarray[{l + 1, r - 1}], candidate);

            // alice left bob left
            candidate = best_this_subarray + piles[l] - piles[l + 1];
            if (best_for_subarray.contains({l + 2, r})) {
                best_for_subarray[{l + 2, r}] = max(best_for_subarray[{l + 2, r}], candidate);
            } else {
                best_for_subarray[{l + 2, r}] = candidate;
                q.push({l + 2, r});
            }

            // alice right bob right
            candidate = best_this_subarray + piles[r] - piles[r - 1];
            if (best_for_subarray.contains({l, r - 2})) {
                best_for_subarray[{l, r - 2}] = max(best_for_subarray[{l, r - 2}], candidate);
            } else {
                best_for_subarray[{l, r - 2}] = candidate;
                q.push({l, r - 2});
            }
        }

        return false;
    }

};
45339