코테/lvl1

프로그래머스 K번째수(lvl1) 풀어보기

디비드킴 2026. 9. 7. 11:11

  문제링크

https://school.programmers.co.kr/learn/courses/30/lessons/42748?language=java

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


문제 접근법

-그냥 


문제 풀이법

배열을 요구사항 요구사항대로 자르고

요구 번호 배열에 담아서 주면끝


코드

public static List<Integer> solution(int[] array, int[][] commands) {

        List<Integer> answer = new ArrayList<>();

        for(int [] command:commands){

            List<Integer>temp=new ArrayList<>();

            for(int i=command[0]-1;i<command[1];i++){

               temp.add(array[i]);

            }

            Collections.sort(temp);

            answer.add(temp.get(command[2]-1));

        }

        return answer;

    }


결과





gpt 피드백

Arrays.copyOfRange 로 배열 복사 바로가능

 for(int [] command:commands){

           int[] temp = Arrays.copyOfRange(

               array,

               command[0] - 1,

               command[1]

            );

            Arrays.sort(temp);

            answer.add(temp[command[2]-1]);

        }

개선하니 for문 줄어들어 가독성이 좋음


배운점
편리한 프리셋을 한번 물어보자