프로그래머스 - k번째 수

phoenixKim·2021년 8월 14일
0

소스코드

#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 begin = commands[i][0];
        int end = commands[i][1];
        int target = commands[i][2];
        
        int size = end - begin + 1;
        vector<int>v(size);
        
        int index = 0;
        for(int j = begin -1; j <= end -1; j++)
            v[index++] = array[j]; 
        
        sort(v.begin(), v.end());
        answer.push_back(v[target -1]);
    }
    
    
    return answer;
}

알게 된점

1. vector 초기화할때

vectorv(v1.begin(), v1.end()) 이렇게 복사를 해왔다.
그래서 index값을 넣을수 있지 않을까 생각했는데 안된다.

  • 벡터 초기화
    : 이런식으로 가능하다.
    하지만 + 1을 더 진행해야 한다.

2. sort 초기화할때

sort(v.begin(), v.end()) 이런식으로 했다.

이터레이터인 v.begin()을 이용하자!

profile
🔥🔥🔥

0개의 댓글

관련 채용 정보