[programmers] K번째수

KwonSC·2022년 4월 11일
0

programmers - Java

목록 보기
12/17
post-thumbnail

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


Code

import java.util.*;

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

Solution

commands를 순회돌며 i, j, k를 가져오고 array를 복사해 Arrays.sort()함수를 이용해 부분 배열 정렬후 원하는 위치의 값을 answer에 넣는다.

0개의 댓글