我有一個算法,創建兩個堆,minHeap和maxHeap。兩者之間的唯一區別是maxHeap顛倒了minHeap的符號,這是一種簡單的黑客方式,將Python的heapq數據結構用作最大堆。下面是我創建的堆代碼(堆關鍵基本上是工人在字典一週的某一天數):奇怪的行爲堆積和負數
for day in self.weekDict:
if day != 'Saturday' and len(self.weekDict[day]) != 0: #saturdays and holidays not part of optimization
heapq.heappush(minHeap, (len(self.weekDict[day]), day))
heapq.heappush(maxHeap, (-len(self.weekDict[day]), day))
的minHeap作品一樣預期,但最大堆給我奇當存在多於一個相同的密鑰時的行爲。見下:
[(-8, 'Thursday'), (-7, 'Monday'), (-5, 'Friday'), (-7, 'Wednesday'), (-7, 'Tuesday')]
爲什麼最近兩天出現故障?是否因爲只有第一天保證是最低限度的,並且一旦我第一天離開堆會自動調整自己?