본문 바로가기
Algorithm

[프로그래머스] 42748번 K번째수 C++

by SoyeonCha 2024. 11. 12.

문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42748

 

메모

- sort 문법

 

맞힌 코드

#include <bits/stdc++.h>

using namespace std;

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