K번째수

seheeee_97·2023년 12월 20일

회고팀

목록 보기
33/41






import java.util.Arrays;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for(int a=0; a<commands.length; a++) {
            int i = commands[a][0];         //i번째부터
            int j = commands[a][1];         //j번째까지
            int k = commands[a][2];         //k번째의 수
            int[] cut = new int[j-i+1];     //i번부터 j까지 자른 수를 담을 배열 선언
            
            int index=0;       //cut배열의 index
            
            for(int c=i-1; c<j; c++) {      //cut배열에 i번째부터 j번째까지 저장
                cut[index] = array[c];
                index++;
            }
            Arrays.sort(cut);
            answer[a] = cut[k-1];       //정렬한 배열의 k번째 수를 answer에 저장
        }
        return answer;
    }
}





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

0개의 댓글