- 이중 반복문으로 데이터를 잘라서 slice 배열에 넣어준다
- slice 배열을 오름차순으로 정렬하고 answer에 k번째 수를 정답 배열에 넣어준다.
/*
* 프로그래머스 Lv1 - K번째 수
* 문제링크: https://programmers.co.kr/learn/courses/30/lessons/42748
*/
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int n = 0;
for(int i = 0; i<commands.length; i++) {
int[] slice = new int[commands[i][1] - commands[i][0]+1];
for (int j = 0; j < slice.length; j++) {
slice[j] = array[j + commands[i][0]-1];
}
Arrays.sort(slice);
answer[i] = slice[commands[i][2]-1];
}
return answer;
}
}
최근에 풀었던 문제들 중에서 난이도가 꽤 어려웠다. 데이터를 추출하는 알고리즘을 짜는 부분에서 시간이 꽤 걸렸던 것 같다. 긴 시간의 노력 끝에 문제를 해결할 수 있었다. 아직도 알고리즘을 짜는 실력이 부족한 것을 다시 한 번 느꼈고 더 공부를 해야겠다는 계기가 된 것 같다.