(Python, Java) 프로그래머스 - K번째수

[문제 링크]

Python 풀이

def solution(array, commands):
    answer = []

    for command in commands:
        partition = array[command[0]-1:command[1]]
        partition.sort()
        answer.append(partition[command[2]-1])
    return answer

Java 풀이

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int idx = 0;

        for (int[] command : commands) {
            int start = command[0] - 1;
            int end = command[1];
            int key = command[2] - 1;
            int[] split = Arrays.copyOfRange(array, start, end);

            Arrays.sort(split);
            answer[idx] = split[key];
            idx += 1;
        }
        return answer;
    }

}

© 2021. By Backtony