-
정렬 알고리즘 문제 : 프로그래머스 LEVEL 1(k번째 수)
- 사용 언어 : C++ - 해결 날짜 : 2021-01-15 **- 해결 방법** : commands vector를 돌며 각 명령 속 i,j,k의 값들을 저장한다. 인자로 주어진 array vector에서 i번째부터 j번째까지 잘라 새 vector arr에 저장한 다음 sort함수로 오름차순 정렬을 수행한 다. 마지막으로 정렬된 arr 배열의 k번째 인덱스의 값을 answer 배열에 추가함으로 문제를 해결하였다. - 코드 ``` #include <string> #include <vector> #include <algorithm> using namespace std; vector<int> solution(vector<int> array, vector<vector<int>> commands) { vector<int> answer; for (int idx = 0; idx < commands.size(); idx++) { int i = commands[idx][0]-1; int j = commands[idx][1]-1; int k = commands[idx][2]-1; //i번째부터 j번째 까지 잘라 arr vector에 저장 vector<int> arr; for (int temp = i; temp <= j; temp++) { arr.push_back(array[temp]); } //오름차순 정렬 sort(arr.begin(), arr.end()); //k번째 값을 answer vector에 추가 answer.push_back(arr[k]); } return answer; } ```