본문 바로가기
Problem Solving/Baekjoon

[백준] 25631 마트료시카 합치기 - Data Structure / Java

by graycode 2024. 4. 1.

 문제 링크

 

25631번: 마트료시카 합치기

마트료시카는 속이 비어있는 인형이다. 성빈이는 $N$개의 마트료시카를 가지고 있다. $i$번째 마트료시카의 크기는 $a_i$이고, 마트료시카 속은 모두 비어있다. 성빈이는 남아 있는 마트료시카 중

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(), a;
        while (n-- > 0) map.put(a = read(), map.getOrDefault(a, 0) + 1);

        int max = 0;
        for (int i : map.values()) max = Math.max(max, i);

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

    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;
    }

}

댓글