나의 풀이
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i< commands.length; i++) {
int[] arr1 = Arrays.copyOfRange(array, commands[i][0] - 1, commands[i][1]);
Arrays.sort(arr1);
int k = commands[i][2];
list.add(arr1[k - 1]);
}
return list.stream().mapToInt(i -> i).toArray();
}
}
▶ copyofRange() 함수를 이용하자~~~
다른 사람의 풀이
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;
}
}
▶ 리스트를 사용하지 않아서 리스트를 배열로 바꾸어 주는 변환 작업이 필요 없음★
'프로그래머스(자바) > LV.1(자바)' 카테고리의 다른 글
삼총사 →3중 for문 +조합★★ (0) | 2022.12.17 |
---|---|
숫자 문자열과 영단어→ map자료형 or 배열2개+replaceAll() (0) | 2022.12.17 |
문자열 내 마음대로 정렬하기★★★→ 애당초 사전순서대로 정렬 + subString() (1) | 2022.12.17 |
최소직사각형→2차원 리스트를 1차원 리스트★★ (0) | 2022.12.16 |
예산→오름차순 정렬★ + 작은 수부터 더하기★ (0) | 2022.12.16 |