개발 공부 기록
728x90

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

간단히 Collection.sort 를 이용하면 되는 문제다.

import java.util.ArrayList;
import java.util.Collections;

public class NumOfKth {
    public static int[] solution(int[] array, int[][] commands) {

        int[] answer = new int[commands.length];

        for(int i = 0; i < commands.length; i ++) {
            ArrayList<Integer> list = new ArrayList<>();
            for(int j = commands[i][0] - 1; j < commands[i][1]; j ++) {
                list.add(array[j]);
            }
            Collections.sort(list);
            answer[i] = list.get(commands[i][2] - 1);
        }

        return answer;
    }

    public static void main(String[] args) {
        int[] array = {1, 5, 2, 6, 3, 7, 4};
        int[][] commands = {{2, 5, 3}, {4, 4, 1}, {1, 7, 3}};
        int[] answer = solution(array, commands);
        for(int a : answer) System.out.print(a + " ");
    }
}
728x90
profile

개발 공부 기록

@찐만두

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!