Algorithm/PROGRAMMERS[Java]

Lv1. K번째 수

씨니 2022. 4. 24. 19:43
728x90

▶ 문제

 

▶ 풀이방법

import java.util.*;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        int num = 0;
        for(int i = 0; i < commands.length; i++){
            int first = commands[i][0]; //첫번째 위치
            int last = commands[i][1];//마지막위치
            int location = commands[i][2]; //원하는 위치
            int[] arr = new int[last-first+2];
            
            for(int j = 0; j <= last-first; j++){ //배열에 first~last위치의 값 넣기
                arr[j] = array[j+first-1];
            }
            Arrays.sort(arr);//오름차순 정렬
            
            answer[num] = arr[location];
            num++;
        }
        
        return answer;
    }
}
728x90