반응형

 

출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

 

 

 

 

 

** Javascript

 

function solution(array, commands) {
    var answer = [];
    for (const command of commands) {
        const sliced = array.slice(command[0]-1, command[1]);
        sliced.sort(function(a,b){return a-b});
        answer.push(sliced[command[2]-1]);
    }
    return answer;
}

 

 

 

** Python

 

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

 

 

 

** Java

 

import java.util.Arrays;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        for (int i = 0; i < commands.length; i++) {
            int[] slice = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
            Arrays.sort(slice);
            answer[i] = slice[commands[i][2]-1];
        }
        return answer;
    }
}

 

반응형

+ Recent posts