알고리즘
[프로그래머스]K번째 수(JAVA)
서부추
2021. 3. 21. 21:56
반응형
코딩테스트 연습 - K번째수 | 프로그래머스 (programmers.co.kr)
코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int count=0; count<commands.length; count++){
int i,j,k;
i = commands[count][0] -1 ;
j = commands[count][1];
k = commands[count][2]-1;
int [] cmd = Arrays.copyOfRange(array, i, j);
Arrays.sort(cmd);
answer[count] = cmd[k];
}
return answer;
}
}
반응형