
문제 설명
- array를 i번째부터 j번째까지 자릅니다.
- 잘려나온 배열을 정렬합니다.
- 정렬된 배열의 k번째 값을 구합니다.
접근법
- 배열을 그대로 사용할 수도 있고 List로 변환해 사용할 수도 있습니다.
- List를 활용하면
subList,sort등 적절한 메서드를 사용할 수 있어 훨씬 간편햐 집니다.
정답
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int cnt = 0;
for (int[] comm : commands) {
int i = comm[0];
int j = comm[1];
int k = comm[2];
int[] new_arr = new int[j-i+1];
int idx = 0;
for (int l = i-1; l < j; l++) {
new_arr[idx++] = array[l];
}
Arrays.sort(new_arr);
int a_answer = 0;
for (int l = 0; l < new_arr.length; l++) {
if (l == k-1) {
a_answer = new_arr[l];
}
}
answer[cnt++] = a_answer;
}
return answer;
}
}