< 문제링크 >
https://www.welcomekakao.com/learn/courses/30/lessons/42748
< 풀이 >
1. 주어진 범위에 대해서 sort 함수를 사용하여 k번째수를 answer에 추가한다.
2. 주의할 점은 k번째 수의 인덱스는 (k-1) 이라는 것이다.
< 코드 >
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for (auto tmp : commands) {
int i = tmp[0], j = tmp[1], k = tmp[2];
vector<int> arr(array);
vector<int>::iterator iter = arr.begin();
// i번째 원소의 실제 인덱스는 (i-1)임에 유의!
sort(iter+i-1, iter+j);
answer.push_back(arr[(i-1) + (k-1)]);
}
return answer;
}
int main(void)
{
vector<int> array = { 1, 5, 2, 6, 3, 7, 4 };
vector<vector<int>> commands = { {2, 5, 3},{4, 4, 1},{1, 7, 3} };
solution(array, commands);
return 0;
}
'알고리즘 > Programmers' 카테고리의 다른 글
Programmers / Sort / H-index (0) | 2020.09.07 |
---|---|
Programmers / Sort / 가장 큰 수 (0) | 2020.09.07 |
Programmers / Stack, Queue / 프린터 (0) | 2020.09.06 |
Programmers / Stack, Queue / 다리를 지나는 트럭 (0) | 2020.09.06 |
Programmers / Stack, Queue / 기능개발 (0) | 2020.09.06 |