본문 바로가기
Problem Solving/Baekjoon

[백준] 18294 Biodiversity - Data Structure / Java

by graycode 2023. 11. 4.

 문제 링크

 

18294번: Biodiversity

Alicia has a huge garden which is the habitat of many animals that she really cares about. After listening to a podcast about biodiversity, she becomes very concerned about the balance between species in her garden. She wants to know if there is a species

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

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

        String key, res = "";
        int n = Integer.parseInt(br.readLine()), max = 0;
        for (int i = 0; i < n; i++) {
            map.put(key = br.readLine(), map.getOrDefault(key, 0) + 1);

            if (max < map.get(key)) {
                max = map.get(key);
                res = key;
            }
        }

        bw.write(max > n / 2 ? res : "NONE");
        bw.flush();
    }

}

댓글