미소의 세상

[알고리즘] 정렬-K번째수 본문

알고리즘/알고리즘 풀이

[알고리즘] 정렬-K번째수

짱미소 2022. 2. 28. 21:18

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

 

코딩테스트 연습 - K번째수

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

돌려가면서 작성

import java.util.Arrays;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
    int [] answer = new int[commands.length];

        for(int i = 0; i < commands.length; ++i){
            int k = 0;

            int [] temp = new int[commands[i][1] - (commands[i][0]-1)];

            for(int j = commands[i][0] -1; j <= commands[i][1] -1 ; ++j){
                    temp[k] = array[j];
                    k++;
            }

            Arrays.sort(temp);
            answer[i] = temp[commands[i][2] -1];
        }

        return answer;
    }
}
Comments