본문 바로가기
Algorithm

K번째수_프로그래머스

by SuldenLion 2024. 2. 29.
반응형

 

 

정수형 배열 array 값들이 주어지고, 그 array에서 잘라낼 첫 번째 인덱스와 마지막 인덱스가 이차형 정수 배열 commands의 commands[i][0], commands[i][1]로써 주어진다.

 

잘라낸 수의 배열을 만들어 정렬한 후, 뽑아내고 싶은 숫자의 인덱스가 commands[i][2]로 주어진다.

 

 

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int idxForAnswer = 0;
        
        for (int i = 0; i < commands.length; i++) {
            int[] tmp = new int[commands[i][1]-commands[i][0]+1];            
            int idx = 0;
                        
            for (int j = commands[i][0]-1; j < commands[i][1]; j++) {
                tmp[idx] = array[j];
                idx++;
            }
            
            Arrays.sort(tmp);
            
            answer[idxForAnswer] = tmp[commands[i][2]-1];
            idxForAnswer++;
        }
        
        return answer;
    }
}

 

반응형

댓글