<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p>

<p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> otherwise</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [2,3,1,1,4]
<strong>Output:</strong> true
<strong>Explanation:</strong> Jump 1 step from index 0 to 1, then 3 steps to the last index.
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,2,1,0,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
</pre>

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

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

class Solution {
public:
    // This is a classic recursive question:
    // Suppose that at position k we have the number m.
    // So the question is whether we can reach the last index from position k+1, up to k+m.
    // If we receive a positive answer then we can abort the recursion.
    // In order to be able to heuristically finish faster, we can work in a greedy way, by first
    // checking k+m.
    // Finally, dynamic programmic is very relevant here since if we got a wrong answer for some index, 
    // then we do not want to ask again the same question where the answer is already known.
    bool canJump(vector<int>& nums) {
        // optimize
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        // edge cases
        int size = (int)nums.size();
        if (size == 0) {
            return false;
        }
        if (size == 1) {
            return true;
        }

        vector<bool> dp(size, true);
        return canJump(nums, 0, dp);
    }

    bool canJump(vector<int>& nums, int index, vector<bool>& dp) {
        // jump to next, if we overshoot then we can definitely reach the last element by choosing
        // an appropriate smaller jump size
        int maxJump = nums[index];
        int maxNextIndex = index + maxJump;
        if (maxNextIndex >= nums.size() - 1) {
            return true;
        }
        for (int nextIndex = index + 1; nextIndex <= maxNextIndex; ++nextIndex) {
            if (dp[nextIndex] && canJump(nums, nextIndex, dp)) {
                return true;
            }
        }
        /*while (nextIndex > index) {
            //cout << "Attempting jump from index " << index << " to index " << nextIndex << endl;
            if (dp[nextIndex] && canJump(nums, nextIndex, dp)) {
                //cout << "Jump success from index " << index << " to " << nextIndex << endl;
                return true;
            }
            --nextIndex;
        }*/

        //cout << "Jump fail at index " << index << endl;
        dp[index] = false;
        return false;
    }
};
53600