from heapq I want to update the Kth value of the heap. -> rewrite, then _siftdown can be used to return to heap state with O(logN)
from heapq import _siftdown
queue = [1, 2, 3]
K = 1
queue[K] = -1
_siftdown(queue, 0, K)
print(queue) # => [-1, 1, 3]
This page is auto-translated from /nishio/ヒープのK番目の値を更新したい using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.