프로그래머스 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;
}
채점 결과
'Coding Test > programmers' 카테고리의 다른 글
프로그래머스 42747 - H-Index (c++) (0) | 2020.07.29 |
---|---|
프로그래머스 42746 - 가장 큰 수 (c++) (0) | 2020.07.29 |
프로그래머스 42587 - 프린터 (c++) (0) | 2020.07.28 |
프로그래머스 42583 - 다리를 지나는 트럭 (c++) (0) | 2020.07.28 |
프로그래머스 42586 - 기능개발 (c++) (0) | 2020.07.28 |