Description Link to heading

55.jump-game

Solution Link to heading

Traversing from back to front, if nums[j] can be achieved from nums[j - 1] or nums in the front, then destination change from nums[j] to nums[j - 1] or num in the front.

Code Link to heading

#include <vector>
using std::vector;
class Solution {
  public:
    bool canJump(vector<int> &nums) {
        int r = nums.size() - 1;
        for (int i = nums.size() - 2; i >= 0; i--) {
            while (i >= 0 && nums[i] + i >= r) {
                r = i;
                i--;
            }
        }
        if (r != 0)
            return false;
        else
            return true;
    }
};