알고리즘 공부/프로그래머스
프로그래머스 코딩테스트 연습 - 정렬 - K번째수
HRuler
2020. 9. 16. 18:01
1. 나의 풀이
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int [] answer = new int[commands.length];
ArrayList <Integer> imsi = new ArrayList<Integer>();
for(int i = 0; i < commands.length; i = i + 1) {
for(int j = commands[i][0]; j <= commands[i][1]; j = j + 1) {
imsi.add(array[j-1]);
}
imsi.sort(Comparator.naturalOrder());
answer[i] = imsi.get(commands[i][2]-1);
imsi.clear();
}
return answer;
}
}
2. 다른 사람 풀이
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;
}
}