1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    
    int ArraySize = array.size();
    int CommandSize = commands.size();
    
    for(int i = 0 ; i < CommandSize ; ++i)
    {
        int MinCommand = commands[i][0-1;
        int MaxCommand = commands[i][1-1;
        int CheckNumb = commands[i][2-1;
        
        if(MinCommand == MaxCommand)
        {
            answer.emplace_back(array[MinCommand]);
            continue;
        }
 
        vector<int> vecTemp;
        
        for(int j = MinCommand ; j <= MaxCommand ; j++)
        {
            vecTemp.emplace_back(array[j]);
        }
 
        sort(vecTemp.begin(), vecTemp.end());
 
        answer.emplace_back(vecTemp[CheckNumb]); 
        
    }
        
    return answer;
}
 

 

1. 배열은 0부터 시작하기떄문에 커맨드 숫자 -1

2. for문 사용 시, Min 값과 Max 값이 같으면 for문이 돌지 않기때문에 예외처리

3. 알고리즘 라이브러리의 Sort() 사용

4. 정답 배열에 집어넣음

+ Recent posts