알고리즘

자바 - 프로그래머스 - 로또의 최고 순위와 최저 순위 (최적화 하기)

Won's log 2024. 4. 23. 15:21

프로그래머스에서 로또의 최고 순위와 최저 순위를 풀었다. 

문제는 어렵지 않았으나 최적화된 코드를 고민해 보는 시간을 가졌고 이번 블로그는 그 내용을 다뤘다.

 

문제와 예시는 다음과 같다.

 

해당 문제에 대한 접근 방법은 생각보다 용이했다.

🤓 코드 설계

1. lottos와 win_nums의 같은 값이 있다면 카운트한다.

2. 0의 개수를 카운트한다.

3. 같은 값의 개수는 최저 순위, 같은 값의 개수+0의 개수가 최고 순위가 된다.

 

🤨 구현한 코드 (복잡한 로그 주의⛔️)

그렇게...구현한 나의 코드는 else if의 성지가 되었다...

package ImplementationAlgorithm;

import java.io.IOException;
import java.util.Arrays;

public class Main {
    public static void main(String[] args)throws IOException {
        int[] lottos = {44, 1, 0, 0, 31, 25};
        int[] win_nums = {31, 10, 45, 1, 6, 19};
        int[] temp = new int[lottos.length];
        int[] answer = new int[2]; // 당첨 가능한 최고 순위와 최저 순위 1~6

        // 같은 수 찾기
        int sameNum = 0;
        int zero = 0;
        for(int i=0; i<lottos.length; i++) {
            if(lottos[i]==0) {
                zero++;
            }
            for(int j=0; j<win_nums.length; j++) {
                if(lottos[i]==win_nums[j]) {
                    temp[sameNum++] = lottos[i];
                }
            }
        }
        // sameNum = 최저 순위
        if(sameNum==6) {
            answer[1] = 1;
        } else if(sameNum==5) {
            answer[1] = 2;
        } else if(sameNum==4) {
            answer[1] = 3;
        } else if(sameNum==3) {
            answer[1] = 4;
        } else if(sameNum==2) {
            answer[1] = 5;
        } else {
            answer[1] = 6;
        }

        // sameNum + 0의 개수 = 최고 순위
        if(sameNum+zero==6) {
            answer[0] = 1;
        } else if(sameNum+zero==5) {
            answer[0] = 2;
        } else if(sameNum+zero==4) {
            answer[0] = 3;
        } else if(sameNum+zero==3) {
            answer[0] = 4;
        } else if(sameNum+zero==2) {
            answer[0] = 5;
        } else {
            answer[1] = 6;
        }

        System.out.println(Arrays.toString(answer));
    }
}

 

위의 코드로도 값은 구해졌다. 그러나 속도와 메모리 용량 사용이 컸다.

무엇보다.. 저 코드를 쓰자니 효율성이 전혀 없어 보여 부끄러웠다.

그래서 case문을 사용하고 싶었는데 어떻게 사용하는지 기억이 나지 않았다. 오래간만에 복습을 하였다.

그러다가 다른 사람의 코드를 참고하게 되어 가져와보았다.

 

😯 다른 사람의 코드

    public int getGrade(int n) {
        switch(n) {
            case 6 :
                return 1;
            case 5 :
                return 2;
            case 4 :
                return 3;
            case 3 :
                return 4;
            case 2 :
                return 5;
            default :
                return 6;
        }
    }

 

😎 수정된 코드

package ImplementationAlgorithm;

import java.io.IOException;
import java.util.Arrays;

public class Main {
    public static void main(String[] args)throws IOException {
        int[] lottos = {44, 1, 0, 0, 31, 25};
        int[] win_nums = {31, 10, 45, 1, 6, 19};
        int[] temp = new int[lottos.length];
        int[] answer = new int[2]; // 당첨 가능한 최고 순위와 최저 순위 1~6

        // 같은 수 찾기
        int sameNum = 0;
        int zero = 0;
        for(int i : lottos) {
            if(i==0) {
                zero++;
            }
            for(int j : win_nums) {
                if(i==j) {
                    sameNum++;
                }
            }
        }
        answer[0] = rank(sameNum+zero);
        answer[1] = rank(sameNum);

        System.out.println(Arrays.toString(answer));
    }
    public static int rank(int n) {
        switch(n) {
            case 6 :
                return 1;
            case 5 :
                return 2;
            case 4 :
                return 3;
            case 3 :
                return 4;
            case 2 :
                return 5;
            default :
                return 6;
        }
    }
}