문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/120880
제한 사항
입출력 예
풀이
import java.util.Comparator;
import java.util.stream.IntStream;
class Solution {
public int[] solution(int[] numlist, int n) {
return IntStream.range(0, numlist.length).boxed()
.sorted(Comparator.comparing((Integer i) -> Math.abs(n - numlist[i]))
.thenComparing((Integer i, Integer j) -> numlist[j] - numlist[i]))
.mapToInt(i -> numlist[i]).toArray();
}
}
후기
정렬 기준을 두 개 잡아야 하는 문제이다. Comparator의 comparing() 후 thenComparing()으로 n에서 뺐을 때 같은 값을 정렬하자.
'코딩테스트 (프로그래머스) > Java' 카테고리의 다른 글
[프로그래머스][JAVA][Lv. 2] 모음사전 (0) | 2023.10.07 |
---|---|
[프로그래머스][JAVA][Lv. 0] 전국 대회 선발 고사 (0) | 2023.10.06 |
[프로그래머스][JAVA][Lv. 0] 문자열 밀기 (0) | 2023.10.04 |
[프로그래머스][JAVA][Lv. 0] 배열 만들기 2 (0) | 2023.10.03 |
[프로그래머스][JAVA][Lv. 0] 다항식 더하기 (0) | 2023.10.02 |