Coding
[프로그래머스][Level1][(정렬)K번째수]
루루지
2021. 3. 7. 14:10
반응형
코드
public class Solution {
public int[] solution(int[] array, int[][] commands) {
int answer[] = new int[commands.length];
for(int i=0; i<commands.length; i++) {
// array를 commands의 0~1인덱스 만큼 추출한 배열을 담기위해 선언
int[] arr = new int[(commands[i][1]-commands[i][0])+1];
// arr 인덱스 변수 선언
int cnt = 0;
for(int j=commands[i][0]-1; j<commands[i][1]; j++) {
arr[cnt] = array[j];
cnt++;
}
// 오름차순 정렬
Arrays.sort(arr);
answer[i] = arr[commands[i][2]-1];
}
return answer;
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] array = {1, 5, 2, 6, 3, 7, 4};
int[][] commands = {{2, 5, 3},{4, 4, 1},{1, 7, 3}};
System.out.println(sol.solution(array, commands));
}
}