728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이)
import java.io.*;
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i<commands.length; i++){
ArrayList<Integer> arr = new ArrayList<>();
int start = commands[i][0]-1;
int end = commands[i][1]-1;
int find = commands[i][2]-1;
for(int idx=start; idx<=end; idx++){
arr.add(array[idx]);
}
Collections.sort(arr);
answer[i] = arr.get(find);
}
return answer;
}// solution
}
범위에 맞춰서 list에 저장해주고 collections.sort를 사용했다.
다른 풀이를 보니, CopyOfRange도 있더라!!
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];
}
이렇게 활용할 수도 있다는 것!!
정렬 기초문제!!
728x90
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 최소직사각형(JAVA) (0) | 2023.04.12 |
---|---|
프로그래머스 체육복(JAVA) (0) | 2022.12.19 |