Why does the while loop in the pop() implementation check index < self.realSize
120

In the Python and Java implementations on https://leetcode.com/explore/featured/card/heap/643/heap/4017/ , line 60 for Python and 74 for Java, it looks like the while loop is doing a useless comparison:

(index < self.realSize and index <= self.realSize // 2)

Why not just compare index <= self.realSize // 2? Is there some edge case I'm missing?

Maybe if self.realSize == 0, but it always starts with index = 1, so it will never start the loop. It also won't start the loop if self.realSize == 1, because 1 > 1//2 which is 1 > 0.

Comments (0)