python学习笔记5-堆

题目链接

  • heapify(q) 初始化一个列表q成为小根堆
  • 这道题取反使之成为大根堆
  • heappop(q) 弹出堆顶
  • heappush(q, e) 将e插入堆中
python 复制代码
class Solution:
    def maxKelements(self, nums: List[int], k: int) -> int:
        q = [-x for x in nums]
        heapify(q)

        ans = 0
        for _ in range(k):
            x = heappop(q)
            ans += -x
            heappush(q, -((-x + 2) // 3))
        return ans
相关推荐
木子杳衫1 天前
【软件开发】管理类系统
python·web开发
程序员小远1 天前
银行测试:第三方支付平台业务流,功能/性能/安全测试方法
自动化测试·软件测试·python·功能测试·测试工具·性能测试·安全性测试
猫头虎1 天前
如何查看局域网内IP冲突问题?如何查看局域网IP环绕问题?arp -a命令如何使用?
网络·python·网络协议·tcp/ip·开源·pandas·pip
沿着路走到底1 天前
python 基础
开发语言·python
烛阴1 天前
武装你的Python“工具箱”:盘点10个你必须熟练掌握的核心方法
前端·python
杨枝甘露小码1 天前
Python学习之基础篇
开发语言·python
我是华为OD~HR~栗栗呀1 天前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
小蕾Java1 天前
PyCharm 软件使用各种问题 ,解决教程
ide·python·pycharm
Lucky_Turtle1 天前
【PyCharm】设置注释风格,快速注释
python
kunge1v51 天前
学习爬虫第四天:多任务爬虫
爬虫·python·学习·beautifulsoup