알고리즘 풀이/프로그래머스

프로그래머스 - K번째수 (정렬)

배게 2019. 12. 4. 15:59
728x90



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
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
 
        for (int i = 0; i < commands.length; i++) {
            int a = commands[i][0- 1;
            int b = commands[i][1- 1;
            ArrayList<Integer> arr = new ArrayList<>();
 
            for (int j = a; j <= b; j++) {
                arr.add(array[j]);
            }
 
            Collections.sort(arr);
 
            answer[i] = arr.get(commands[i][2- 1);
 
            // for(int k : arr){
            //     System.out.println(k);
            // }
            // System.out.println("====");
 
        }
 
        return answer;
    }
cs