https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
# 코드
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int idx = 0; idx < commands.length; idx++) {
int i = commands[idx][0];
int j = commands[idx][1];
int k = commands[idx][2];
int[] arr = new int[j-i+1];
for (int a = 0; a < j-i+1; a++) {
arr[a] = array[i-1+a];
}
Arrays.sort(arr);
answer[idx] = arr[k-1];
}
return answer;
}
}
# ✨ 프로그래머스 1등 코드 ✨
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i<commands.length; i++){
int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
Arrays.sort(temp);
answer[i] = temp[commands[i][2]-1];
}
return answer;
}
- Arrays.copyOfRange(원본배열, 시작인덱스, 끝인덱스) : 원본배열의 [시작인덱스 ~ 끝인덱스 - 1] 까지를 복사해서 새로운 배열로 리턴
- 즉, 끝 인덱스는 포함되지 않는다. (start <= index < end)
'코딩테스트 > 자바 문제풀이' 카테고리의 다른 글
[프로그래머스: 완전탐색] 피로도 (0) | 2025.04.25 |
---|---|
[프로그래머스: 완전탐색] 카펫 (0) | 2025.04.25 |
[프로그래머스: 완전탐색] 소수 찾기 (0) | 2025.04.24 |
[프로그래머스: 완전탐색] 모의고사 (0) | 2025.04.24 |
[프로그래머스: 완전탐색] 최소직사각형 (0) | 2025.04.24 |