From 09169299ff23431594b90f805573db12e4377724 Mon Sep 17 00:00:00 2001 From: SeokHo-Ham Date: Tue, 13 Dec 2022 13:03:33 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B7=A4=20=EA=B3=A0=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\355\217\254\355\202\244/20221212.java" | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "\355\217\254\355\202\244/20221212.java" diff --git "a/\355\217\254\355\202\244/20221212.java" "b/\355\217\254\355\202\244/20221212.java" new file mode 100644 index 0000000..af2e60a --- /dev/null +++ "b/\355\217\254\355\202\244/20221212.java" @@ -0,0 +1,34 @@ +package level2; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +class 귤고르기 { + public int solution(int k, int[] tangerine) { + int answer = 0; + + Map map = new HashMap<>(); + + for (int i = 0; i < tangerine.length; i++) { + map.put(tangerine[i], map.getOrDefault(tangerine[i], 0) + 1); + } + + List collect = map.entrySet().stream() + .sorted((a, b) -> b.getValue() - a.getValue()) + .map(Entry::getKey) + .collect(Collectors.toList()); + + int i = 0; + + while(k > 0) { + k -= map.get(collect.get(i)); + answer++; + i++; + } + + return answer; + } +} \ No newline at end of file