码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题——2558. Take Gifts From the Richest Pile


    文章目录

      • 一、题目
      • 二、题解

    一、题目

    2558. 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:

    Choose the pile with the maximum number of gifts.
    If there is more than one pile with the maximum number of gifts, choose any.
    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 <= 103
    1 <= gifts[i] <= 109
    1 <= k <= 103

    二、题解

    由于每次都需要重复“取最大值”的操作,因此使用最大堆进行存储,优化时间复杂度

    class Solution {
    public:
        long long pickGifts(vector<int>& gifts, int k) {
            priority_queue<int> q(gifts.begin(),gifts.end());
            while(k--){
                int tmp = q.top();
                q.pop();
                q.push(int(sqrt(tmp)));
            }
            long long res = 0;
            while(!q.empty()){
                res += q.top();
                q.pop();
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    洛谷P1406 方格填数 题解
    凭什么Java程序员的工资那么高,原来要“啃透”这999页阿里P7学习笔记
    删除安装Google Chrome浏览器时捆绑安装的Google 文档、表格、幻灯片、Gmail、Google 云端硬盘、YouTube网址链接(Mac)
    docker容器的detached模式下查看logs
    一、创建线程的三种方式
    彻底理解Java并发:Java并发原子类
    地图可视化:基于 Echarts + 百度地图bmap + 时间轴timeline + 多边形(multi)polygon + 点scatter 的可视化案例
    进程控制——进程等待
    Netty源码剖析之数据通信流程
    高速电路设计-前言
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/134087445
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号