K번째수


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

문제보기
Alt text

소스코드

import java.util.Arrays;

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

        for (int cmmd = 0; cmmd < commandSize; cmmd++) {
            int[] currentCmmd = commands[cmmd];

            int i = currentCmmd[0] - 1;
            int j = currentCmmd[1] - 1;
            int k = currentCmmd[2] - 1;

            int arrayIdx = i;
            int[] newArray = new int[j - i + 1];
            for (int start = 0; start < newArray.length; start++) {
                newArray[start] = array[arrayIdx++];
            }

            Arrays.sort(newArray);
            answer[cmmd] = newArray[k];
        }

        return answer;
    }
}