본문 바로가기
Problem Solving/Baekjoon

[백준] 3022 PRASE - Data Structure / Java

by graycode 2023. 5. 4.

 문제 링크

 

3022번: PRASE

The first line of input contains an integer N (1 ≤ N ≤ 100), how many pieces of food the children take.  Each of the following N lines contains the name of a child that took one piece of food. The names will be strings of at most 20 lowercase letters

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

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

public class Main {

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

        int n = Integer.parseInt(br.readLine());

        Map<String, Integer> map = new HashMap<>();

        int cnt = 0;
        for (int i = 0; i < n; i++) {
            String key = br.readLine();
            int value = map.getOrDefault(key, 0);
            if (value > i - value)
                cnt++;

            map.put(key, value + 1);
        }

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

}

댓글