https://programmers.co.kr/learn/courses/30/lessons/42748
import java.util.*;
class Solution {
public static int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
List<Integer> arr_list = new ArrayList<>();
for (int a : array) {
arr_list.add(a);
}
for (int i = 0; i < commands.length; i++) {
List<Integer> new_list = new ArrayList<>(arr_list);
new_list = new_list.subList(commands[i][0] - 1, commands[i][1]);
Collections.sort(new_list);
answer[i] = new_list.get(commands[i][2] - 1);
}
return answer;
}
}
import java.util.*;
class Solution {
public static int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
int[] temp = Arrays.copyOfRange(array, commands[i][0] - 1, commands[i][1]);
Arrays.sort(temp);
answer[i] = temp[commands[i][2] - 1];
}
return answer;
}
}
처음엔 subList
를 사용하여 자르고 복사했는데 Arrays.copyOfRange
를 사용하니 더욱 간편했다.
문제마다 적절한 것을 사용할 수 있게끔 연습해야겠다.