알고리즘 & 자료구조/프로그래머스
K번째수
by 신재권
2022. 10. 29.
package programmers;
import java.util.Arrays;
public class K번째수 {
public static int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
int[] sliceArray = Arrays.copyOfRange(array, commands[i][0] - 1, commands[i][1]);
Arrays.sort(sliceArray);
answer[i] = sliceArray[commands[i][2] - 1];
}
return answer;
}
public static void main(String[] args) {
System.out.println(
Arrays.toString(solution(new int[] {1, 5, 2, 6, 3, 7, 4}, new int[][] {{2, 5, 3}, {4, 4, 1}, {1, 7, 3}})));
}
}