Kth Missing Positive Number

ㅋㅋ·2023년 3월 6일
0

알고리즘-leetcode

목록 보기
125/135

0보다 큰 정수가 담긴 벡터 arr과 정수 k를 받게 된다.

벡터 arr에 숫자가 1부터 저장됐을거리 기대했을 때 없는 숫자들 중 k번째의 숫자를 찾는 문제

class Solution {
public:
    int findKthPositive(vector<int>& arr, int k) {
        
        int expectation{1};
        int index{0};
        while (0 < k)
        {
            if (arr.size() <= index || expectation != arr[index])
            {
                --k;
            }
            else
            {
                ++index;
            }

            ++expectation;
        }

        return expectation - 1;
    }
};

0개의 댓글