728x90
안녕하세요.
도저히 기본기가 부족하다는 생각에 핀의 추천으로 프로그래머스 알고리즘Kit을 풀기 시작했습니다.
정렬파트의 K번째수 문제를 풀었습니다.
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] res = new int[commands.length];
for (int i=0; i<commands.length; i++) {
int[] command = commands[i];
int start = command[0];
int end = command[1];
int pick = command[2];
List<Integer> picks = new ArrayList();
for(int j=start-1; j<=end-1; j++) {
picks.add(array[j]);
}
picks.sort(Comparator.naturalOrder());
res[i] = picks.get(pick-1);
}
return res;
}
}
O(n^2)의 시간복잡도를 가진 풀이입니다.
이 문제를 풀면서 배운 점이 있습니다.
- import는 *로 한다.
- 답의 수는 commands의 행의 개수이므로, 배열의 크기는 정적임을 알 수 있다.
뭔가 하나씩 더 배우네요. 파이팅입니다!
728x90
'츄Log > 알고리즘 연습장' 카테고리의 다른 글
연습#6 H-Index (1) | 2023.12.06 |
---|---|
연습#5 가장 큰 수 (1) | 2023.12.06 |
연습#4 폰켓몬 (1) | 2023.12.05 |
연습#3 완주하지 못한 선수 (1) | 2023.12.04 |
연습#1 599. Minimum Index Sum of Two Lists (0) | 2023.12.03 |