본문 바로가기

알고리즘 문제풀이

[Programmers] 프로그래머스 Lv.1 K번째수 문제풀이(C++)

반응형

 

단순구현 문제

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <vector>
#include<algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    
    for(int ii=0; ii<commands.size(); ii++){
        int i=commands[ii][0], j=commands[ii][1], k=commands[ii][2];
        
        vector<int> list;
        for(int a=i-1; a<=j-1; a++){
            list.push_back(array[a]);
        }
        
        sort(list.begin(), list.end());
        
        answer.push_back(list[k-1]);
    }
    
    return answer;
}
cs

반응형