Concise C++ Solution(BFS, 16ms, 20 lines)


  • 1
    H
    class Solution {
    public:
        int jump(vector<int>& nums) {
            int level = 0;
            int cur_begin = 0;
            int cur_end = 0;
            int next_end = 0;
            while (cur_end < nums.size() - 1) {
                // search [cur_begin, cur_end]
                for (int index = cur_begin; index <= cur_end; ++index) {
                    next_end = max(next_end, index + nums[index]);
                }
                // move to next level.
                ++level;
                cur_begin = cur_end + 1;
                cur_end = next_end;
            }
            return level;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.