[c++/알고리즘] 프로그래머스 k번째수

corncheese·2021년 7월 25일
0

알고리즘문제풀이

목록 보기
14/31


나의 코드

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    vector<int> temp;

    for(int i = 0; i < commands.size() ;i++){
        temp.insert(temp.end(), array.begin()+commands[i][0]-1, array.begin()+commands[i][1]);
        sort(temp.begin(), temp.end());
        answer.push_back(temp[commands[i][2]-1]);
        temp.clear();
    }

    return answer;
}
  1. 백터를 잘라서 임시배열에 넣기
// 임시배열에 배열을 잘라서 넣는다. 
// 2번째 숫자부터 5번째 수를 넣으려면 -> 2<= iter < 6
// array.begin() + ( 2 - 1 ) / array.begin() + (5)
temp.insert(temp.end(), array.begin()+commands[i][0]-1, array.begin()+commands[i][1]);
  1. 백터 정렬하기
 sort(temp.begin(), temp.end());
  1. 임시배열 값 삭제하기
temp.clear();

0개의 댓글