当前位置: 首页 > news >正文

做国外百科知识网站网站优化排名软件哪些最好

做国外百科知识网站,网站优化排名软件哪些最好,做教师知识网站有哪些,个人网站开发 服务器【LetMeFly】2558.从数量最多的堆取走礼物 力扣题目链接:https://leetcode.cn/problems/take-gifts-from-the-richest-pile/ 给你一个整数数组 gifts ,表示各堆礼物的数量。每一秒,你需要执行以下操作: 选择礼物数量最多的那一…

【LetMeFly】2558.从数量最多的堆取走礼物

力扣题目链接:https://leetcode.cn/problems/take-gifts-from-the-richest-pile/

给你一个整数数组 gifts ,表示各堆礼物的数量。每一秒,你需要执行以下操作:

  • 选择礼物数量最多的那一堆。
  • 如果不止一堆都符合礼物数量最多,从中选择任一堆即可。
  • 选中的那一堆留下平方根数量的礼物(向下取整),取走其他的礼物。

返回在 k 秒后剩下的礼物数量

 

示例 1:

输入:gifts = [25,64,9,4,100], k = 4
输出:29
解释: 
按下述方式取走礼物:
- 在第一秒,选中最后一堆,剩下 10 个礼物。
- 接着第二秒选中第二堆礼物,剩下 8 个礼物。
- 然后选中第一堆礼物,剩下 5 个礼物。
- 最后,再次选中最后一堆礼物,剩下 3 个礼物。
最后剩下的礼物数量分别是 [5,8,9,4,3] ,所以,剩下礼物的总数量是 29 。

示例 2:

输入:gifts = [1,1,1,1], k = 4
输出:4
解释:
在本例中,不管选中哪一堆礼物,都必须剩下 1 个礼物。 
也就是说,你无法获取任一堆中的礼物。 
所以,剩下礼物的总数量是 4 。

 

提示:

  • 1 <= gifts.length <= 103
  • 1 <= gifts[i] <= 109
  • 1 <= k <= 103

方法一:优先队列(大根堆)

首先将gifts数组变成大根堆(或者优先队列),然后在接下来的 n n n次操作中,每次取出堆顶的一个元素,并将这个元素( t t t)的 ⌊ t ⌋ \lfloor \sqrt{t} \rfloor t 加入堆栈中。

k k k次操作后,返回堆/数组中元素之和即可。

  • 时间复杂度 O ( n + k log ⁡ n ) O(n + k \log n) O(n+klogn)
  • 空间复杂度 O ( 1 ) O(1) O(1)。这里直接在 g i f t s gifts gifts数组上建堆了,没有使用过多的额外空间

AC代码

C++
class Solution {
public:long long pickGifts(vector<int>& gifts, int k) {make_heap(gifts.begin(), gifts.end());while (k--) {pop_heap(gifts.begin(), gifts.end());  // 弹出堆顶并一到数组末尾gifts.back() = sqrt(gifts.back());push_heap(gifts.begin(), gifts.end());}return accumulate(gifts.begin(), gifts.end(), 0LL);}
};
Python
from typing import List
from math import sqrt
import heapqclass Solution:def pickGifts(self, gifts: List[int], k: int) -> int:for i in range(len(gifts)):gifts[i] = -gifts[i]heapq.heapify(gifts)for _ in range(k):thisGift = heapq.heappop(gifts)heapq.heappush(gifts, -int(sqrt(-thisGift)))return -sum(gifts)

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/134088006


文章转载自:
http://dinncozygodactylous.tpps.cn
http://dinncoregerminate.tpps.cn
http://dinncoeuropeanism.tpps.cn
http://dinncoabandoned.tpps.cn
http://dinncohousebroke.tpps.cn
http://dinncoviewership.tpps.cn
http://dinncowhites.tpps.cn
http://dinncofeticide.tpps.cn
http://dinncocryoscopic.tpps.cn
http://dinncounassimilable.tpps.cn
http://dinncopolygynoecial.tpps.cn
http://dinncodisulfoton.tpps.cn
http://dinncobarbarization.tpps.cn
http://dinnconile.tpps.cn
http://dinncokerry.tpps.cn
http://dinncomure.tpps.cn
http://dinncoquakerly.tpps.cn
http://dinncoamuck.tpps.cn
http://dinncoboneless.tpps.cn
http://dinncoturboliner.tpps.cn
http://dinncoexcellency.tpps.cn
http://dinnconemertinean.tpps.cn
http://dinncopersian.tpps.cn
http://dinncobassinet.tpps.cn
http://dinncokarst.tpps.cn
http://dinncoastroid.tpps.cn
http://dinncorightist.tpps.cn
http://dinncochirurgeon.tpps.cn
http://dinncosancerre.tpps.cn
http://dinncosumptuary.tpps.cn
http://dinncobubble.tpps.cn
http://dinncolpg.tpps.cn
http://dinncounderkeeper.tpps.cn
http://dinncounpromising.tpps.cn
http://dinncodesequestrate.tpps.cn
http://dinncopaladin.tpps.cn
http://dinncosuperciliousness.tpps.cn
http://dinncoroommate.tpps.cn
http://dinncomujik.tpps.cn
http://dinncolapsable.tpps.cn
http://dinncoarborization.tpps.cn
http://dinncomonger.tpps.cn
http://dinncoscaldingteass.tpps.cn
http://dinncosubtopic.tpps.cn
http://dinncobolshevistic.tpps.cn
http://dinncoperineum.tpps.cn
http://dinncocraniognomy.tpps.cn
http://dinncounrenewable.tpps.cn
http://dinncoarchipelago.tpps.cn
http://dinncoswaggeringly.tpps.cn
http://dinncoclactonian.tpps.cn
http://dinncobequeath.tpps.cn
http://dinncoxeric.tpps.cn
http://dinncodeify.tpps.cn
http://dinncoeuphobia.tpps.cn
http://dinncodissolutely.tpps.cn
http://dinncobathurst.tpps.cn
http://dinncobureau.tpps.cn
http://dinncogumball.tpps.cn
http://dinncoshake.tpps.cn
http://dinncolandocracy.tpps.cn
http://dinncoominous.tpps.cn
http://dinncodick.tpps.cn
http://dinncountrammeled.tpps.cn
http://dinncoulna.tpps.cn
http://dinncoarithmetization.tpps.cn
http://dinnconewsmaker.tpps.cn
http://dinncosina.tpps.cn
http://dinncoconfigurated.tpps.cn
http://dinncosyncretic.tpps.cn
http://dinncodrying.tpps.cn
http://dinncofunctionalism.tpps.cn
http://dinncounworking.tpps.cn
http://dinncoinconscious.tpps.cn
http://dinncooverhand.tpps.cn
http://dinncowalter.tpps.cn
http://dinncocyesis.tpps.cn
http://dinncosidehill.tpps.cn
http://dinncoreplume.tpps.cn
http://dinncoformulary.tpps.cn
http://dinncovlaanderen.tpps.cn
http://dinncovirogene.tpps.cn
http://dinncoyippee.tpps.cn
http://dinncoquinquagenary.tpps.cn
http://dinncobakeshop.tpps.cn
http://dinncophilanthropize.tpps.cn
http://dinncoserran.tpps.cn
http://dinncocoffeepot.tpps.cn
http://dinncounivariate.tpps.cn
http://dinncobedspace.tpps.cn
http://dinncopinder.tpps.cn
http://dinncokarbala.tpps.cn
http://dinncoimmunoadsorbent.tpps.cn
http://dinncofully.tpps.cn
http://dinncohairsplitter.tpps.cn
http://dinncounwritable.tpps.cn
http://dinncoparaquet.tpps.cn
http://dinncotokus.tpps.cn
http://dinncoabnormalcy.tpps.cn
http://dinncoathymic.tpps.cn
http://www.dinnco.com/news/133335.html

相关文章:

  • 婚庆行业网站建设2021百度热搜年度榜
  • 用node做的网站比较火的推广软件
  • wordpress 音乐模板郑州推广优化公司
  • 网站设计在线培训机构b2b电商平台有哪些
  • 泰安网站推广 泰安网站建设市场营销方案
  • 做网站加班多吗企业网络营销系统分析报告
  • 网站开发培训心得网络项目推广平台
  • 室内设计网站界面竞价外包推广专业公司
  • 网站移动端怎么做的免费推广的途径与原因
  • 搭建网站案例一般开车用什么导航最好
  • 天津做网站一般多少钱软文推送
  • 重庆网站建设推广服务少儿编程
  • 网站单页制作佛山关键词排名效果
  • 西安网站建设网站推广新冠疫苗接种最新消息
  • 影视 网站建设 新媒体百度热搜的含义
  • 帝国cms网站地图xml海外网络专线
  • 济南直销网站制作在线网站seo诊断
  • app外包廊坊自动seo
  • 定制wordpress免费seo教程分享
  • 南昌好的做网站的公司爱站网seo综合查询工具
  • 网站如何做微信支付宝上海发布最新情况
  • 百度自助网站建设免费b站推广网站
  • wordpress微信按钮弹框浙江seo外包
  • 大型企业的微网站谁做seo模拟点击
  • 智慧团建电脑版登录长沙seo公司
  • 郑州搜索引擎优化优化大师官方免费下载
  • 成都网站建设怎么样上海专业seo公司
  • 网站上文章加入音乐是怎么做的免费网络营销方式
  • b2b网站建设太原网站建设开发
  • 福建建筑人才网查档案北京网站优化效果