Algorithm 🧑🏻💻/프로그래머스(Programmers)
[c++] 프로그래머스 - K번째 수 ( Level 1)
dkswnkk
2021. 10. 19. 00:06
코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
#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>split;
int first=commands[i][0];
int second=commands[i][1];
int third=commands[i][2];
for(int k=first; k<=second; k++){
split.push_back(array[k-1]);
}
sort(split.begin(),split.end());
answer.push_back(split[third-1]);
}
return answer;
}