Back to course home
0% completed
Vote For New Content
The max heap solution gives 23 for following inputs:input = [3, 5, 8, 7], k1=2...
Gary
Apr 15, 2022
The max heap solution gives 23 for following inputs:
input = [3, 5, 8, 7], k1=2, k2=7
when the answer should be 15 because it's popping off all elements from max_heap in the last for loop.
I needed to change it to as follows to get the correct answer:
upper_bound = min (k2 - k1 - 1, input_size - k1)
for i in range (upper_bound): if max_heap: sum += -heapq.heappop(max_heap) else: break
If i use k2=9 instead, I get index out of range error from official max heap solution because it tries to pop too many times
0
0
Comments
Comments
Design Gurus4 years ago
This is not a valid input. If the array has only four elements, the array can't have 7th smallest element (as k2=7).