반응형
정수형 배열 array 값들이 주어지고, 그 array에서 잘라낼 첫 번째 인덱스와 마지막 인덱스가 이차형 정수 배열 commands의 commands[i][0], commands[i][1]로써 주어진다.
잘라낸 수의 배열을 만들어 정렬한 후, 뽑아내고 싶은 숫자의 인덱스가 commands[i][2]로 주어진다.
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int idxForAnswer = 0;
for (int i = 0; i < commands.length; i++) {
int[] tmp = new int[commands[i][1]-commands[i][0]+1];
int idx = 0;
for (int j = commands[i][0]-1; j < commands[i][1]; j++) {
tmp[idx] = array[j];
idx++;
}
Arrays.sort(tmp);
answer[idxForAnswer] = tmp[commands[i][2]-1];
idxForAnswer++;
}
return answer;
}
}
반응형
'Algorithm' 카테고리의 다른 글
bitCount 메서드 사용 (다음 큰 숫자_프로그래머스) (0) | 2024.02.20 |
---|---|
C++로 BigInteger 구현해보기 (0) | 2023.04.11 |
Algorithm - Merge Sort (병합 정렬) (0) | 2022.08.18 |
Algorithm - Sieve of Eratosthenes(소수판별 알고리즘) (0) | 2022.07.30 |
Algorithm - 크루스칼 알고리즘(Kruskal) (0) | 2022.07.21 |
댓글