Ad
  • Default User Avatar

    Heap is really not good solution in this situation. it does not help the algorithm become all that much more efficient. Using a heap instead would barely show significant gain in performance, bar certain very narrow situations. Using heap here to reduce from O(2n) to O(n), while using a loop is already so elegant and simply, is impractical (in most cases), in short.

    That being said, while this solution is elegant, some understanding of time and space complexity is required for beginners to make the most of it. Blindly following code shortening practices is not good software engineering practice.

  • Custom User Avatar

    I was going to say something similar, it's much more efficient to leverage a heap. Not nearly as pretty tho

  • Custom User Avatar

    Please use spoiler flag next time.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution