-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNumber.java
More file actions
40 lines (31 loc) · 1.11 KB
/
KNumber.java
File metadata and controls
40 lines (31 loc) · 1.11 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
35
36
37
38
39
40
package programmers;
import java.util.ArrayList;
import java.util.Arrays;
public class KNumber {
public static void main(String[] args) {
Solution12 s = new Solution12();
int[] array = {1, 5, 2, 6, 3, 7, 4};
int[][] commands = {{2, 5, 3}, {4, 4, 1}, {1, 7, 3}};
s.solution(array, commands);
}
}
class Solution12 {
public int[] solution(int[] array, int[][] commands) {
// 예를 들어 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입니다.
int[] arr = new int[commands.length];
for(int i=0; i<commands.length; i++) {
int tmp = commands[i][1] - commands[i][0];
int[] new_array = new int[tmp+1];
for(int j = commands[i][0], k = 0; j<=commands[i][1]; j++) {
new_array[k++] = array[j-1];
}
Arrays.sort(new_array);
arr[i] = (new_array[commands[i][2]-1]);
}
return arr;
}
}