코딩 테스트

(Java) 프로그래머스 - 예상 대진표

로승리 2022. 7. 20. 18:06

문제가 꽤 쉬운 편이었다. 특정 알고리즘을 사용하는 것도 아니라서

그냥 문제에 나온대로 코드로 구현했다. 처음에는 HashMap을 이용해서

한번 대진을 돌고나면 새로 번호를 부여하려고 했는데 다시 생각해보니

불필요할 것 같아서 메서드를 수정했다.


최종 코드

class Solution
{
    public int solution(int n, int a, int b)
    {
        int answer = search(n, a, b);
        return answer;
    }
    // 대진 진행 메서드
    static int search(int n, int a, int b) {
        int first = 0;
        int second = 0;
        
        // while문 탈출을 위한 a, b 대소 빅교
        if (a > b) {
            first = b;
            second = a;
        } else {
            first = a;
            second = b;
        }
        int cnt = 1;
                // a와 b가 만나려면 작은수가 홀수이고 두 수의 차이가 1

        while (!(first % 2 != 0 && second - first == 1)) {
            first = (first + 1) / 2;
            second = (second + 1) / 2;
            cnt++;
        }

        return cnt;
    }
}