알고리즘/프로그래머스
[프로그래머스] 정렬 - k번째수
땀두
2022. 3. 23. 08:29


import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0;i<commands.length;i++){
int[] ary = new int [commands[i][1]-commands[i][0]+1];
for(int j=0;j<ary.length;j++){
ary[j] = array[commands[i][0]-1+j];
}
Arrays.sort(ary);
answer[i] = ary[commands[i][2]-1];
}
return answer;
}
}
간단히 commands에 들어있는 2번째수에서 1번째 수를 뺀 값에 1을 더한만큼의 크기의 배열을 생성하고, 해당 인덱스에 해당하는 array에 있는 값들을 넣는다. 그 이후 그 값들을 sort메소드를 통해 정렬하고, commands 배열의 마지막 값에 해당하는 인덱스 값을 출력하면 해결할 수 있다.