[프로그래머스] K번째수 JAVA 풀이

권용환·2021년 8월 24일
0

programmers_level1

목록 보기
13/14
post-thumbnail

문제 바로가기

나의 풀이

Java는 배열을 활용하는 문제에서 참 불편하다. Arrays.copyOfRange()를 활용했다면 for문을 많이 줄일 수 있을 것으로 보인다.

import java.util.*;

class Solution {
    public static int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int count = 0;
        for (int[] command : commands) {
            int[] temp = new int[command[1] - command[0] + 1];
            for (int i = command[0]; i <= command[1]; i++) {
                temp[i - command[0]] = array[i-1];
            }
            Arrays.sort(temp);
            answer[count++] = temp[command[2] - 1];
        }
        return answer;
    }
}
profile
마구 낙서하는 블로그입니다

0개의 댓글