max-heap

BUILD-MAX-HEAP running time for array sorted in decreasing order

主宰稳场 提交于 2019-12-24 05:54:24
问题 I know that the running time for BUILD-MAX-HEAP in heap sort is O(n) . But, if we have an array that already sorted in a decreasing order, why do we still have O(n) for the running time of BUILD-MAX-HEAP? Isn't it supposed to be something like O(1) ? It's already sorted from the maximum value to the minimum value, so we do not need MAX-HEAPIFY. Is my understanding correct? Could someone please explain it to me? 回答1: You are right. It can of course be O(1) . When you know for sure that your

BUILD-MAX-HEAP running time for array sorted in decreasing order

和自甴很熟 提交于 2019-12-24 05:54:11
问题 I know that the running time for BUILD-MAX-HEAP in heap sort is O(n) . But, if we have an array that already sorted in a decreasing order, why do we still have O(n) for the running time of BUILD-MAX-HEAP? Isn't it supposed to be something like O(1) ? It's already sorted from the maximum value to the minimum value, so we do not need MAX-HEAPIFY. Is my understanding correct? Could someone please explain it to me? 回答1: You are right. It can of course be O(1) . When you know for sure that your

How to maintain a Java TreeMap size to be a constant while adding key-value pairs into the TreeMap?

假如想象 提交于 2019-12-12 03:04:08
问题 CODE IS HERE is a nice and simple example of TreeMap in java to keep track of the key-value pairs added to the map in a sorted order. However, I am unsure on how to ensure that I only keep 10 items in the TreeMap. How to ensure that the size of the TreeMap is always a constant, like 10? So the original problem is: to keep track of the top ten key-value pairs weighted by value in the TreeMap while the stream of key-value keeps coming in. I want to see how the to write the code in java to

Pop max value from a heapq python, Is there a max-heap in Python? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-11-30 16:28:07
Possible Duplicate: What do I use for a max-heap implementation in Python? I am trying to implement in some way the heapq of python but for a max-heap. A solution is using the (-1) and multiple with numbers of the queue but that doesn't help me as I need to store urls in the heap. So i want a max heapq where i can pop the largest value. Wrap the objects in a reverse-comparing wrapper: import functools @functools.total_ordering class ReverseCompare(object): def __init__(self, obj): self.obj = obj def __eq__(self, other): return isinstance(other, ReverseCompare) and self.obj == other.obj def _

Pop max value from a heapq python, Is there a max-heap in Python? [duplicate]

走远了吗. 提交于 2019-11-29 23:34:10
问题 Possible Duplicate: What do I use for a max-heap implementation in Python? I am trying to implement in some way the heapq of python but for a max-heap. A solution is using the (-1) and multiple with numbers of the queue but that doesn't help me as I need to store urls in the heap. So i want a max heapq where i can pop the largest value. 回答1: Wrap the objects in a reverse-comparing wrapper: import functools @functools.total_ordering class ReverseCompare(object): def __init__(self, obj): self