티스토리 뷰

https://programmers.co.kr/learn/courses/30/lessons/42748

 

알고리즘 연습 - K번째수 | 프로그래머스

실행 결과가 여기에 표시됩니다.

programmers.co.kr

 

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
 
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int num = 0;
        
        for(int n = 0; n < commands.length; n++){
            int i = commands[n][0];
            int j = commands[n][1];
            int k = commands[n][2];
            
            int[] temp = new int[j-(i-1)];  //sorting 할 것들 임시로 담아두는 배열
            int cnt = 0;
            
            for(int p = i - 1; p < j; p++) {
                temp[cnt] = array[p];
                cnt++;
                }
               Arrays.sort(temp);
               answer[num] = temp[k-1];
               num++;
            }
          return answer;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
댓글