티스토리 뷰

 

 

K번째수 문제는 주어진 array 배열을 commands 이차원 배열의 값을 통해 각각 min, max, index 값을 추출하여 min부터 max까지 배열 복사를 정렬을 통해 index 값의 위치를 추출하면 된다.

import java.util.Arrays;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        
        int cLength = commands.length;
        int[] answer = new int[cLength];
        
        for (int i=0; i<cLength; i++) {
            
            int min = commands[i][0];
            int max = commands[i][1];
            int index = commands[i][2];
            
            //Arrays 범위 복사
            int[] range = Arrays.copyOfRange(array, min-1, max);
            //정렬
            Arrays.sort(range);
            answer[i] = range[index-1];
        }
        
        return answer;
    }
}

'프로그래밍 > 프로그래머스' 카테고리의 다른 글

[프로그래머스] 2016년  (0) 2019.10.05
[프로그래머스] 완주하지 못한 선수  (0) 2019.10.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
링크
«   2024/05   »
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
글 보관함