Jump Game II

ㅋㅋ·2023년 2월 8일
0

알고리즘-leetcode

목록 보기
106/135

인덱스를 기준으로 뛰어넘을 수 있는 거리가 적힌 nums 정수형 벡터를 받는다.

0번째 인덱스에서 시작하여 마지막 인덱스까지 점프를 할 때, 최소 점프수를 구하는 문제

class Solution {
public:
    int jump(vector<int>& nums) {
        
        int length = nums.size();
        int target{length - 1};

        int position{0};
        int nextMax{nums[0]};
        int counter{0};
        while (position < target)
        {
            int localMax{0};
            for (int i = position + 1; i <= nextMax && i < length; ++i)
            {
                localMax = std::max(localMax, i + nums[i]);
            }
            
            position = nextMax;
            nextMax = localMax;
            ++counter;
        }

        return counter;
    }
};

0개의 댓글