Programming/Programmers

프로그래머스 K번째수 c++

fishersheep 2022. 3. 11. 15:52
반응형
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    
    vector<int>temp;
    int start,end,num;
    
    for(int i=0;i<commands.size();i++)
    {   
        for(int k=0;k<temp.size();k++)
            temp.clear();
        
        start = commands[i][0];
        end = commands[i][1];
        num = commands[i][2];
        
        for(int j=start-1;j<end;j++)
            temp.push_back(array[j]);
        
        sort(temp.begin(),temp.end());
        
        answer.push_back(temp[num-1]);
    }
    
    return answer;
}
반응형