지우너

[프로그래머스] k번째수 C++ 본문

Problem Solving

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

지옹 2025. 2. 21. 22:58

문제

https://school.programmers.co.kr/learn/courses/30/lessons/42748

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

풀이 및 코드

vector에도 substring이 있을 거 같은데, 몰라서 그냥 for문을 돌면서 직접 넣어줬다.

#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++){
        int start = commands[i][0]-1; // 0-based
        int end = commands[i][1]-1;
        int idx = commands[i][2]-1;
        
        vector<int> tmp;
        for(int j=start; j<=end; j++){
            tmp.push_back(array[j]);
        }
        sort(tmp.begin(), tmp.end());
        
        answer.push_back(tmp[idx]);
    }
    return answer;
}