guodongxiaren / OJ

4 stars 3 forks source link

LeetCode 45: 跳跃游戏|| #36

Open guodongxiaren opened 4 years ago

guodongxiaren commented 4 years ago

https://leetcode-cn.com/problems/jump-game-ii

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

你的目标是使用最少的跳跃次数到达数组的最后一个位置。

示例:

输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
     从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。

说明:

假设你总是可以到达数组的最后一个位置。

guodongxiaren commented 4 years ago

贪心,太难了。

题解:https://leetcode-cn.com/problems/jump-game-ii/solution/45-by-ikaruga/

class Solution {
public:
    int jump(vector<int> &nums)
    {
        int ans = 0;
        int start = 0;
        int end = 1;
        while (end < nums.size()) {
            int maxPos = 0;
            for (int i = start; i < end; i++) {
                // 能跳到最远的距离
                maxPos = max(maxPos, i + nums[i]);
            }
            start = end;      // 下一次起跳点范围开始的格子
            end = maxPos + 1; // 下一次起跳点范围结束的格子
            ans++;            // 跳跃次数

        }
        return ans;
    }
};