Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags more
Archives
Today
Total
관리 메뉴

개발의변화

프로그래머스 LV3 억억단을 외우자 본문

알고리즘

프로그래머스 LV3 억억단을 외우자

refindmySapporo 2024. 2. 15. 08:50
반응형
     Arrays.sort(arr, new Comparator<int[]>() {

         @Override
         public int compare(int[] o1, int[] o2) {
                int answer = o2[1] - o1[1];
                if (answer == 0) {
                    answer = o1[0] - o2[0];
                }
                return answer;
         }
    });

https://school.programmers.co.kr/learn/courses/30/lessons/138475?language=java

[프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr](https://school.programmers.co.kr/learn/courses/30/lessons/138475?language=java)

약수를 활용해야 하는 문제였다.

물론 약수를 하나씩 구해서 2차원 배열로 나타낼 수 있었지만 더 빠른 방법을 착안했다.

        int[] test = new int[e+1];

        for (int i=1; i<=e; i++) {
            for (int j=i; j<=e; j += i) {
                test[j]++;
            }
        }

1

e까지의 숫자의 배수를 구하면서 1

e까지 걸리는 횟수를 세준다

그리고 나서 주어진 조건처럼 배열의 정렬을 해준다.

Arrays.sort(arr, new Comparator<int>() {


        @Override
        public int compare(int[] o1, int[] o2) {
            int answer = o2[1] - o1[1];
            if (answer == 0) {
                answer = o1[0] - o2[0];
            }
            return answer;
        }
    });
반응형