From da959699dde56e3316bb85a9b0fabf584e4c0b45 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 12 Dec 2024 22:52:23 +0100 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB2558.=20Take=20Gifts=20From?= =?UTF-8?q?=20the=20Richest=20Pile=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/take-gifts-from-the-richest-pile/ Signed-off-by: Matej Focko --- java/take-gifts-from-the-richest-pile.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 java/take-gifts-from-the-richest-pile.java diff --git a/java/take-gifts-from-the-richest-pile.java b/java/take-gifts-from-the-richest-pile.java new file mode 100644 index 0000000..af52f04 --- /dev/null +++ b/java/take-gifts-from-the-richest-pile.java @@ -0,0 +1,19 @@ +class Solution { + public long pickGifts(int[] gifts, int k) { + var q = new PriorityQueue(Comparator.reverseOrder()); + for (var gift : gifts) { + q.offer(gift); + } + + for (int i = 0; i < k; ++i) { + int richest = q.poll(); + q.offer((int) Math.sqrt(richest)); + } + + long remaining = 0; + while (!q.isEmpty()) { + remaining += q.poll(); + } + return remaining; + } +}