[programmers] K번째수

JongSeong Yang·2021년 5월 10일
0

programmers

목록 보기
8/16

문제 풀이 : 2021.05.10

풀이

command 별로 임의의 배열을 만들어서 sort함수 적용

코드

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

문제 출처 링크

profile
꿈꾸는 개발자

0개의 댓글