-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution11.java
More file actions
34 lines (30 loc) · 1.24 KB
/
Solution11.java
File metadata and controls
34 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* 코딩 테스트 공부 - 11
* 배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
* 예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
* array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
* 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
* 2에서 나온 배열의 3번째 숫자는 5입니다.
*/
package codingTest;
import java.util.Arrays;
public class Solution11 {
public static int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i = 0; i < commands.length; i++) {
int[] copy = new int[commands[i][1] - commands[i][0] + 1];
int k = 0;
for(int j = commands[i][0] - 1; j < commands[i][1]; j++ ) {
copy[k] = array[j];
k++;
}
Arrays.sort(copy);
answer[i] = copy[commands[i][2] - 1];
}
return answer;
}
public static void main(String[] args) {
int[] arr = {1, 5, 2, 6, 3, 7, 4};
int[][] commands = { {2, 5, 3}, {4, 4, 1}, {1, 7, 3} };
System.out.println(solution(arr, commands));
}
}