首页 > 其他分享 >2558. Take Gifts From the Richest Pile

2558. Take Gifts From the Richest Pile

时间:2023-02-05 23:45:16浏览次数:52  
标签:gifts 2558 number Gifts chosen behind pile Take Pile

package LeetCode_2558

import java.util.*

/**
 * 2558. Take Gifts From the Richest Pile
 * https://leetcode.com/problems/take-gifts-from-the-richest-pile/
 * You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:
1. Choose the pile with the maximum number of gifts.
2. If there is more than one pile with the maximum number of gifts, choose any.
3. Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.
Return the number of gifts remaining after k seconds.

Example 1:
Input: gifts = [25,64,9,4,100], k = 4
Output: 29
Explanation:
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.

Example 2:
Input: gifts = [1,1,1,1], k = 4
Output: 4
Explanation:
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
That is, you can't take any pile with you.
So, the total gifts remaining are 4.

Constraints:
1 <= gifts.length <= 10^3
1 <= gifts[i] <= 10^9
1 <= k <= 10^3
 * */
class Solution {
    /*
    * Solution: Greedy, use PriorityQueue to keep tracking the largest num.
    * Time complexity:O(nlogn)
    * Space complexity:O(n)
    * */
    fun pickGifts(gifts: IntArray, k: Int): Long {
        val maxHeap = PriorityQueue<Int> { a, b -> b - a }
        for (item in gifts) {
            maxHeap.add(item)
        }
        var count = k
        while (maxHeap.isNotEmpty()) {
            val sqrtElement = Math.sqrt(maxHeap.poll().toDouble())
            maxHeap.add(sqrtElement.toInt())
            if (--count <= 0) {
                break
            }
        }
        var result: Long = 0L
        while (maxHeap.isNotEmpty()) {
            result += maxHeap.poll()
        }
        return result
    }
}

 

标签:gifts,2558,number,Gifts,chosen,behind,pile,Take,Pile
From: https://www.cnblogs.com/johnnyzhao/p/17094201.html

相关文章

  • Keras compile loss metrics
    Thepurposeoflossfunctionsistocomputethequantitythatamodelshouldseektominimizeduringtraining. https://tensorflow.google.cn/api_docs/python/......
  • Jetty - Unable to compile class for JSP
    问题与分析在启动公司项目时发现报错如下:[jetty]2019-10-0710:28:28.760:WARN:org.apache.jasper.compiler.Compiler:Errorcompilingfile:D:\lewis.liu\CBX_KME\Progra......
  • `defineProps` is a compiler macro and no longer needs to be imported.
    通过vite创建的vue3项目,在使用defineProps时,如果通过import从vue中进行了引用,会在终端中出现下面的警告信息:defineProps属于vue3提供的宏,并不需要引入,所以当出现这种警......
  • 解决Error:java: System Java Compiler was not found in classpath
    转载自:https://blog.csdn.net/xq_sq/article/details/77869389===========第一次使用intellij的时候碰到了一个这样的问题:Error:java:SystemJavaCompilerwasnotfou......
  • groovy-eclipse-compiler
    usecompiler默认javac改为groovy-eclipse <plugin><artifactId>maven-compiler-plugin</artifactId><!--2.8.0-01andlaterrequiremaven-compiler-plugi......
  • arc143 C - Piles of Pebbles
    题意:\(n\)堆石子,每堆\(a_i\)个。甲乙轮流操作。甲每次选择至少一堆,使被选堆的石子数都减\(X\);乙每次选择至少一堆,使被选堆的石子数都减\(Y\)。不能操作者输。问谁赢......
  • Compile Sqlite3 Executable, Static Library, and Shared Library on Linux
    DownloadSqlite3sourcecode,anddecompressittosomewhere.Enterthedecompressedfolder,typethefollowingcommandtogeneratedifferenttargetswithall......
  • C++代码与AST compiler
    C++代码与ASTcompilerCompiler3_语法制导翻译&AST语法制导翻译(SyntaxDirectedTranslation)的任务解析输入的字符串时,在特定位置执行指定的动作。基本思想   ......
  • Error:java: Compilation failed: internal java compiler error 解决办法
    编译时提示错误 Error:java:Compilationfailed:internaljavacompilererror 1、查看项目的jdk(Ctrl+Alt+shift+S)File->ProjectStructure->ProjectSettings->......
  • 03-逻辑综合工具 - Design Compiler
    逻辑综合工具DCIC设计流程,市场-->制定spec-->RTL(同时进行sim,通过alint检查RTL有没有错误)-->systhesis(逻辑综合)-->PR(STA)-->TapeOut逻辑综合将RTL转换为GateNetli......