본문 바로가기
Problem Solving/Baekjoon

[백준] 31718 Double Up - Data Structure / Java

by graycode 2024. 4. 2.

 문제 링크

 

31718번: Double Up

작업을 원하는 만큼 수행한 뒤, 배열에서 가장 많이 등장하는 수의 가능한 최대 등장 횟수를 출력한다.

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        Map<Integer, Integer> map = new HashMap<>();
        int n = read(), max = 0;
        while (n-- > 0) {
            int a = getOddFactor(read());
            map.put(a, map.getOrDefault(a, 0) + 1);
            max = Math.max(max, map.get(a));
        }

        bw.write(String.valueOf(max));
        bw.flush();
    }

    private static int getOddFactor(int a) {
        while ((a & 1) == 0) a >>= 1;
        return a;
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글