프로그래머스 [JAVA] :: 특이한 정렬

smi·2023년 1월 24일
0

📚 문제 정의

정수 n을 기준으로 n과 가까운 수부터 정렬하려고 합니다. 이때 n으로부터의 거리가 같다면 더 큰 수를 앞에 오도록 배치합니다. 정수가 담긴 배열 numlist와 정수 n이 주어질 때 numlist의 원소를 n으로부터 가까운 순서대로 정렬한 배열을 return하도록 solution 함수를 완성해주세요.


📝 제한사항

  • 1 ≤ n ≤ 10,000
  • 1 ≤ numlist의 원소 ≤ 10,000
  • 1 ≤ numlist의 길이 ≤ 100
  • numlist는 중복된 원소를 갖지 않습니다.

📝 입출력 예

numlistnresult
[1, 2, 3, 4, 5, 6]4[4, 5, 3, 6, 2, 1]
[10000,20,36,47,40,6,10,7000]30[36, 40, 20, 47, 10, 6, 7000, 10000]

💡 코드

import java.util.*;

class Solution {
    public int[] solution(int[] numlist, int n) {
        int[] answer = new int[numlist.length];
        double[] order = new double[numlist.length];
        
        for(int i = 0; i < numlist.length; i++) {
            if(numlist[i] - n < 0) 
                // n을 뺏을 때 음수 값인 경우, +0.5를 해서 같은 거리에 있는 양수 값보다 정렬을 했을 시 뒤로 밀리도록 함 
                order[i] = ((int)Math.abs(numlist[i] - n)) + 0.5;
            else 
                order[i] = numlist[i] - n; 
        }
        
        Arrays.sort(order); // 오름차순 정렬
        
        for(int i = 0; i < numlist.length; i++) {
            if(order[i] % 1 != 0) answer[i] = n - (int)order[i];
            else answer[i] = (int)order[i] + n;
        }
        
        return answer;
    }
}

profile
공부한 거 올려요 :)

0개의 댓글