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

Peace·2021년 6월 23일

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

문제 접근

단순한 sort문제이다.
그냥 stl에서 제공하는 sort를 해당 부분만큼에만 적용하고, 구하면 된다.

코드 구현(c++)

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    vector<int> vectmpArray;

    for (int i = 0; i < (int)commands.size(); i++)
    {
        vectmpArray.clear();
        for (int j = commands[i][0]-1; j <= commands[i][1]-1; j++)
        {
            vectmpArray.push_back(array[j]);
        }
        sort(vectmpArray.begin(), vectmpArray.end());
        answer.push_back(vectmpArray[commands[i][2]-1]);
    }

    return answer;
}
profile
https://peace-log.tistory.com 로 이사 중

0개의 댓글