Even Idiots Can Make Game

프로그래머스 42748 LV1 K번째 수

Date/Lastmod
Section
PS
Title
Ps-Tags
Solved

아주 쉬운 문제이지만 반복자 관련 복습을 위하여 리뷰 노트를 작성한다.

우선 입력되는 벡터 array가 변경되면 안 되므로 매 command를 적용하기 이전에 복사를 하던 해야한다.

위와 같은 순서로 진행하였다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
  vector<int> answer;
  
  for (auto const& cmd: commands) {
      
    auto rb = next(array.cbegin(), cmd[0] - 1);
    auto re = next(array.cbegin(), cmd[1]);
    
    vector<int> carr(rb, re);
    
    sort(carr.begin(), carr.end());
    
    auto target = *next(carr.cbegin(), cmd[2] - 1);
    answer.push_back(target);
  }
    
    return answer;
}

아래 함수들의 사용법을 잠깐 정리하려고 썼다.

comments powered by Disqus