JAVA/프로그래머스 코딩테스트 입문

[프로그래머스, LV.0] 특이한 정렬

sundori 2024. 12. 15. 18:21

목차

    문제 설명

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


    제한사항

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

     

    numlist n result
    [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) {
            return UniqueSorting(numlist,n);
        }
        //특이한 정렬
        public static int[] UniqueSorting(int[] numlist, int n ){
            // 거리 계산 및 정렬 기준 배열 생성
            int[][] arr = new int[numlist.length][2];
            for (int i = 0; i < numlist.length; i++) {
                arr[i][0] = numlist[i]; // 원래 값
                arr[i][1] = Math.abs(numlist[i] - n); // n과의 거리
            }
    
            // 정렬: 거리 오름차순, 거리가 같으면 값 내림차순
            Arrays.sort(arr, (a, b) -> {
                if (a[1] != b[1]) {
                    return Integer.compare(a[1], b[1]); // 거리 기준 오름차순
                } else {
                    return Integer.compare(b[0], a[0]); // 값 기준 내림차순
                }
            });
    
            // 정렬된 결과에서 원래 값 추출
            int[] result = new int[numlist.length];
            for (int i = 0; i < arr.length; i++) {
                result[i] = arr[i][0];
            }
    
            return result;
        }
    }
    import java.util.Arrays;
    
    public class App {
        public static void main(String[] args) throws Exception {
            // 특이한 정렬 예시
            System.out.println(
                Arrays.toString(
                    UniqueSorting(new int[]{1, 2, 3, 4, 5, 6}, 4)
                )
            );
    
            System.out.println(
                Arrays.toString(
                    UniqueSorting(new int[]{10000, 20, 36, 47, 40, 6, 10, 7000}, 30)
                )
            );
        }
    
        // 특이한 정렬 메서드
        public static int[] UniqueSorting(int[] numlist, int n) {
            // 거리 계산 및 정렬 기준 배열 생성
            int[][] arr = new int[numlist.length][2];
            for (int i = 0; i < numlist.length; i++) {
                arr[i][0] = numlist[i]; // 원래 값
                arr[i][1] = Math.abs(numlist[i] - n); // n과의 거리
            }
    
            // 정렬: 거리 오름차순, 거리가 같으면 값 내림차순
            Arrays.sort(arr, (a, b) -> {
                if (a[1] != b[1]) {
                    System.out.println("Comparing by distance: " + Arrays.toString(a) + " and " + Arrays.toString(b));
                    int result = Integer.compare(a[1], b[1]);
                    System.out.println("Result: " + result);
                    return result; // 거리 기준 오름차순
                } else {
                    System.out.println("Comparing by value: " + Arrays.toString(a) + " and " + Arrays.toString(b));
                    int result = Integer.compare(b[0], a[0]);
                    System.out.println("Result: " + result);
                    return result; // 값 기준 내림차순
                }
            });
    
            // 정렬 후 배열 상태 출력
            System.out.println("Sorted array: ");
            for (int[] x : arr) {
                System.out.println(Arrays.toString(x));
            }
    
            // 정렬된 결과에서 원래 값 추출
            int[] result = new int[numlist.length];
            for (int i = 0; i < arr.length; i++) {
                result[i] = arr[i][0];
            }
    
            return result;
        }
    }