개발자는 기록이 답이다

[프로그래머스][Java][Lv.1] k번째 수 본문

알고리즘/프로그래머스

[프로그래머스][Java][Lv.1] k번째 수

slow-walker 2023. 10. 4. 00:30

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내가 푼 풀이

import java.util.*;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        for (int a = 0; a < commands.length; a++) {
            int i = commands[a][0]; // 2
            int j = commands[a][1]; // 5
            int k = commands[a][2]; // 3
            ArrayList<Integer> list = new ArrayList<>();
            for (int l = i-1; l <= j-1; l++) {
                list.add(array[l]);
            }
            Collections.sort(list);
            int target = list.get(k - 1);
            answer[a] = target;
        }
        return answer;
    }
}
테스트 1 통과 (0.22ms, 66.8MB)
테스트 2 통과 (0.21ms, 65.6MB)
테스트 3 통과 (0.21ms, 72MB)
테스트 4 통과 (0.29ms, 75.5MB)
테스트 5 통과 (0.24ms, 77.1MB)
테스트 6 통과 (0.27ms, 72.3MB)
테스트 7 통과 (0.24ms, 86.3MB)