알고리즘
프로그래머스 K번째 수 C++
fulladdr
2023. 10. 19. 13:56
구현 문제이다. 배열을 부분 배열로 자르고, 정렬한다음에 k번째 수를 리턴하면 된다. 간단하다
시간 제약도 없어서 따로 알고리즘을 쓸 필요가 없다.
#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++){
int start = commands[i][0];
int end = commands[i][1];
int k = commands[i][2];
vector<int> sub_array;
for (int i=start-1;i<end;i++){
sub_array.push_back(array[i]);
}
sort(sub_array.begin(), sub_array.end());
answer.push_back(sub_array[k-1]);
}
return answer;
}
실행 결과