ㅁㄴㅇㄹ

K번쨰 수 (Level 1) 본문

코딩테스트 연습/프로그래머스

K번쨰 수 (Level 1)

Larvar 2021. 7. 24. 17:09

 

별 어려울것 없는 구현 문제였다. vector의 정렬은 algorithm 라이브러리의 sort함수를 사용해 해결하였다.

 

#include <string>
#include <algorithm>
#include <vector>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> ans;

    for (int i = 0; i < commands.size(); i++) {
        vector<int> cmd = commands[i];
        int start = cmd[0]; int end = cmd[1]; int idx = cmd[2];

        vector<int> temp;
        for (int j = start; j <= end; j++) {
            temp.push_back(array[j-1]);
        }

        sort(temp.begin(), temp.end());
        ans.push_back(temp[idx - 1]);
    }

    return ans;
}