332. K번째수

아현·2021년 11월 12일
0

Algorithm

목록 보기
356/400
post-custom-banner

프로그래머스



1. Python


def solution(array, commands):
    answer = []
    for i, j, k in commands:
        answer.append(sorted(array[i - 1: j])[k - 1])
        
    return answer



2. C++


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

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



3. JavaScript



function solution(array, commands) {
    var answer = [];
    let temp;
    for(let [i, j, k] of commands){
        temp = array.slice(i - 1, j);
        temp.sort((a, b) => a - b);
        answer.push(temp[k - 1]);
    }
    return answer;
}



profile
For the sake of someone who studies computer science
post-custom-banner

0개의 댓글