Coding Test - cpp/Sort
[프로그래머스 lv 1 ] - K번째수
에드윈H
2021. 4. 21. 19:53
programmers.co.kr/learn/courses/30/lessons/42748
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for (int i = 0; i < commands.size(); i++)
{
vector<int> temp;
int start = commands[i][0] - 1; //start번째부터
int end = commands[i][1] - 1; //end번째수 자릿수 지정
for (int j = start; j <= end; j++)
{
temp.push_back(array[j]); //저장
}
sort(temp.begin(), temp.end()); //오름차순 정렬
int n = commands[i][2] - 1; //n번째수 저장
answer.push_back(temp[n]);
}
return answer;
}