You can use a tuple instead of HeapItem. Tuple is set as (-count, word). Tuple comparison is resolved on the first element. If first element is equal, then it'll starts to compare the second element, and it happens that it's a mean heap so it's already in lexicographical order.
@seant116 ай бұрын
Can you explain why in your __lt__ the comparison is self.word > to_compare.word? My assumption is that if you're trying to compare if the current word is lexicographically smaller than the "to_compare" word then it would be self.word < to_compare.word?
@keithang93355 ай бұрын
think about it this way: for 2 strings a and b, a>b if a comes after b in the dictionary. e.g “leetcode” > “idiom” # True you want the 0th element of your min heap to be the one with smallest count and shows up last in the dictionary, so you want (self.count==other.count and self.word > other.word) or (self.count < other.count)
@pranjalgupta699513 күн бұрын
I think its because we are reversing the result at the end.
@satwiktatikonda76411 ай бұрын
you channel is very underrated
@crackfaang11 ай бұрын
I agree haha
@prodbyCheez5 ай бұрын
Is the to_compare the item that is already on the heap? Also is __lt__ called when you when you first call HeapItem(word, count)?