[C++]프로그래머스 K번째 수
문제는 다음과 같다.
오늘은 프로그래머스 K번째 수를 풀어보도록 하자. 구현은 쉽다. 정렬 알고리즘과 벡터 사용법만 안다면 어렵지 않게 풀 수 있는 문제였다. 주석을 참고하자. :)
[코드]
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
//답을 저장하는 벡터
vector<int> answer;
//answer에 넣을 임시 저장 벡터
vector<int> temp_answer;
//commands의 길이만큼 for문을 돈다
for(int i=0;i<commands.size();i++){
int from = commands[i][0];
int to = commands[i][1];
int pick_num = commands[i][2];
//임시 저장 벡터에 담는다.
for(int j=from;j<=to;j++){
temp_answer.push_back(array[j-1]);
}
sort(temp_answer.begin(), temp_answer.end());
answer.push_back(temp_answer[pick_num-1]);
temp_answer.clear();
}
return answer;
}