Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:24:16+00:00 2026-06-17T04:24:16+00:00

I have an algorithm that creates two heaps, minHeap and maxHeap. The only difference

  • 0

I have an algorithm that creates two heaps, minHeap and maxHeap. The only difference between the two is that maxHeap reverses the sign of minHeap, a simple hack to use Python’s heapq data structure as a max heap. Here’s my code for creating the heaps (the heap key is basically the number of workers in a dictionary for a given day of the week):

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))

The minHeap works just as expected, but the max heap gives me odd behavior when there are more than one of the same key. See below:

[(-8, 'Thursday'), (-7, 'Monday'), (-5, 'Friday'), (-7, 'Wednesday'), (-7, 'Tuesday')]

Why are the last two days out of order? Is it because only the first day is guaranteed to be the minimum, and once I pop the first day off the heap will automatically adjust itself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:24:17+00:00Added an answer on June 17, 2026 at 4:24 am

    A heap is not a sorted list. A heap is a binary tree which happens to be stored as a list. The elements of a heap have the following property:

    a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all k in a

    See the docs for a fuller explanation and a nice picture to help follow the structure: http://docs.python.org/2/library/heapq.html#theory

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I have simple algorithm that clean the whitespace from half string until end. Here
I have an algorithm that finds the minimum of function/n on Rn. And I
I have an algorithm that finds a value in a cell, for this case
I have an algorithm that recursively makes change in the following manner: public static
I have this algorithm that I want to implement on VB6. Sub Main() dim
If I have an algorithm that takes n log n steps (e.g. heapsort), where
I have written an algorithm that takes geospatial data and performs a number of
I have an algorithm in C++ that uses Kalman Filter. Somewhere in the code
I have to write an algorithm that find the path in DAG with single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.