我在 Python 中使用什么来实现最大堆? [英] What do I use for a max-heap implementation in Python?

查看:20
本文介绍了我在 Python 中使用什么来实现最大堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 包含用于最小堆的 heapq 模块,但我需要一个最大堆.Python 中的最大堆实现应该使用什么?

Python includes the heapq module for min-heaps, but I need a max heap. What should I use for a max-heap implementation in Python?

推荐答案

最简单的方法是反转键的值并使用 heapq.比如把1000.0变成-1000.0,5.0变成-5.0.

The easiest way is to invert the value of the keys and use heapq. For example, turn 1000.0 into -1000.0 and 5.0 into -5.0.

这篇关于我在 Python 中使用什么来实现最大堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆