백준 1475 방 번호 (Java, 자바)

jonghyukLee·2023년 8월 19일
0

이번에 풀어본 문제는
백준 1475번 방 번호 입니다.

📕 문제 링크

❗️코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char [] input = br.readLine().toCharArray();

        int [] numberCnt = new int[9];
        for (char c: input) {
            int n = c - '0';
            if (n == 9) numberCnt[6]++;
            else numberCnt[n]++;
        }

        int tmpValue = numberCnt[6] / 2;
        tmpValue += numberCnt[6] % 2 > 0 ? 1 : 0;

        numberCnt[6] = tmpValue;

        int maxCount = Integer.MIN_VALUE;
        for (int i = 0; i < 9; i++) {
            maxCount = Math.max(maxCount, numberCnt[i]);
        }

        System.out.print(maxCount);
    }
}

📝 풀이

방 번호를 완성하는데 필요한 숫자 세트의 개수를 출력하는 문제입니다.
나열된 각 숫자를 카운트해서 가장 많이 카운트 된 숫자의 개수가 세트의 수가 될 수 있습니다.
하지만, 6과 9를 뒤집어 사용할 수 있다는 조건이 존재하므로, 두 수는 같이 카운트 한 후 나머지값만 체크해주면 간단하게 해결할 수 있습니다.

profile
머무르지 않기!

0개의 댓글