본문 바로가기

코딩테스트

K번째수

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
//https://school.programmers.co.kr/learn/courses/30/lessons/42748
public class K번째수 {
    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}};

        K번째수 k = new K번째수();
        int[] reesult = k.solution(array, commands);
        System.out.println(reesult);
    }

    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        for(int i = 0; i < commands.length; i++){

            int start = commands[i][0];
            int end = commands[i][1];
            int idx = commands[i][2];


            start = start -1;
            end = end -1;
            List<Integer> tmp = new ArrayList<>();
            for(int x = 0; x < array.length; x++){
                if(x >= start && x <= end){
                    tmp.add(array[x]);
                }
            }
            Collections.sort(tmp);
            answer[i] = tmp.get(idx-1);
        }
        return answer;
    }

}

'코딩테스트' 카테고리의 다른 글

[프로그래머스] 머쓱이보다 키 큰 사람  (0) 2025.01.12
[프로그래머스] n의 배수  (0) 2025.01.12
코딩테스트 추억점수  (0) 2024.10.05
완주하지 못한 선수 (해시)  (1) 2024.10.02
같은 숫자는 싫어  (0) 2024.08.12