알고리즘/[2020] 프로그래머스
[프로그래머스] K번째수 - java
Sorrel
2020. 4. 13. 20:40
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 [] list = new int[commands[i][1]-commands[i][0]+1];
int tmp =0;
for(int j = commands[i][0]-1; j<commands[i][1];j++){
list[tmp]=array[j];
tmp++;
}
Arrays.sort(list);
answer[i]=list[commands[i][2]-1];
}
return answer;
}
}
두시간 동안 쩔쩔맸다. 배열 인덱스 참조가 잘못됐다는데 도저히 모르겠어서 찾아봤더니
마지막 코드 answer[i]=list[commands[i][2]-1];를 answer[i]=list[commands[i][2]];로 해서 안 되는 거였다.
어이없는 실수..