프로그래머스 42748 - K번째수
문제 링크
생각 및 접근
- index와 실제 배열에 저장되는 index가 1씩 차이나서 귀찮았던 문제.
- vector temp를 선언해서 array를 새로 담고, commands에서 시키는 대로 원하는 구간을 sort 후, 원하는 답을 answer에 push_back하면 끝나는 문제
코드
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> temp;
vector<int> answer;
for(int i = 0; i < commands.size(); i++){
temp = array;
sort(temp.begin() + commands[i][0] - 1, temp.begin() + commands[i][1]);
answer.push_back(temp[(commands[i][0] - 1) + (commands[i][2] - 1)]);
}
return answer;
}
채점 결과