๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - K๋ฒˆ์งธ ์ˆ˜ ( Level 1)

by ์•ˆ์ฃผํ˜• 2021. 10. 19.
 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - K๋ฒˆ์งธ์ˆ˜

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

 

#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++){
        vector<int>split;
        int first=commands[i][0];
        int second=commands[i][1];
        int third=commands[i][2];
        for(int k=first; k<=second; k++){
            split.push_back(array[k-1]);
        }
        sort(split.begin(),split.end());
        answer.push_back(split[third-1]);
    }    
    return answer;
}

๋Œ“๊ธ€