45.Jump Game II

class Solution {
public:
    int jump2(vector<int>& nums) {
        int res = 0, n = nums.size(), last = 0, cur = 0;
        for (int i = 0; i < n - 1; ++i) {
            cur = max(cur, i + nums[i]);
            if (i == last) {
                last = cur;
                ++res;
                if (cur >= n - 1) break;
            }
        }
        return res;
    }
};
posted @ 2019-04-09 10:02  JohnRed  阅读(70)  评论(0编辑  收藏  举报