1. 내 풀이
System.arraycopy 사용
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int[] list;
for(int i = 0; i < commands.length ; i++) {
list = new int[commands[i][1] - commands[i][0] + 1];
System.arraycopy(array, commands[i][0] - 1, list, 0, commands[i][1] - commands[i][0] + 1);
Arrays.sort(list);
answer[i] = list[commands[i][2] - 1];
}
return answer;
}
}
2. 다른 분 풀이
Arrays.copyOfRange 사용.
import java.util.Arrays;
class Solution {
public 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;
}
}
'코딩테스트 > Programmers' 카테고리의 다른 글
[프로그래머스] 포켓몬 (0) | 2025.03.01 |
---|---|
[프로그래머스] 비밀지도 (0) | 2025.02.28 |
[프로그래머스] 푸드 파이트 대회 (0) | 2025.02.28 |
[프로그래머스] 숫자 문자열과 영단어 (0) | 2025.02.28 |
[프로그래머스] 두 개 뽑아서 더하기 (0) | 2025.02.28 |