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

jaranda·2022년 3월 23일
0

K번째수

결과

문제 풀이

#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> res;
    for(int i=0;i<commands.size();i++)
    {
        vector<int> answer;
        for(int j=commands[i][0];j<=commands[i][1];j++)
        
            answer.push_back(array[j-1]);
        
        sort(answer.begin(),answer.end());
        res.push_back(answer[commands[i][2]-1]);
    }
    return res;
}

i번째부터 j번째까지 자르고 answer 에 넣은뒤 정렬하고 , k번째의 수를 res에 담은뒤 반환했다.

profile
자라는 개발자

0개의 댓글