문제링크
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;
}
결과

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문 줄어들어 가독성이 좋음
배운점
'코테 > lvl1' 카테고리의 다른 글
| 프로그래머스 소수만들기(lvl1) 풀어보기 (0) | 2026.09.07 |
|---|---|
| 프로그래머스 모의고사(lvl1) 풀어보기 (0) | 2026.09.07 |
| 프로그래머스 가장가까운글자(lvl1) 풀어보기 (0) | 2026.09.07 |
| 프로그래머스 같은숫자는 싫어(lvl1) 풀어보기 (0) | 2026.09.07 |
| 프로그래머스 포켓몬(lvl 1) 풀어보기 (0) | 2026.09.07 |