1. 입력 된 배열을 command에 따라 잘라서 정렬하기 쉽게 ArrayList에 담는다. 2. List의 Sort를 써서 오름차순 정렬을 한다. 3. command에서 요구한 순번의 숫자를 get하여 resultArray에 담는다. 4. 이 과정을 input command만큼 반복한다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public int[] solution(int[] array, int[][] commands) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int loop = commands.length;
int[] result = new int[loop];
int cnt = 0;
for(int[] command : commands){
int start = command[0];
int end = command[1];
int point = command[2]-1;
ArrayList<Integer> arrayList = new ArrayList<>();
for(int i = start-1; i<end; i++){
arrayList.add(array[i]);
}
// 오름 차순 정렬
// 내림 차순 정렬은 Comparator.reverseOrder()
arrayList.sort(Comparator.naturalOrder());
result[cnt] = arrayList.get(point);
cnt++;
}
return result;
}
}