티스토리 뷰

알고리즘/C++

[C++] 프로그래머스 K번째 수

지휘리릭 2020. 2. 25. 18:22
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    vector<int> temp;
    
    for(int i = 0;i<commands.size(); i++){
        temp = array;
        sort(temp.begin()+commands[i][0]-1, temp.begin()+commands[i][1]);
        answer.push_back( temp[commands[i][0] + commands[i][2] - 2]);
    }
    return answer;
}

주의해야할 점

sort 에서 첫번째 인자 시작점부터 시작해서 두번째 인자 끝점 바로 전에 끝난다.

0 부터 10 까지 정렬하고 싶다면 sort(0, 11)으로 해야한다.

댓글