Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-348

Heap Build Comparison Count

Problem

Count element comparisons in the bottom-up heapify build of a min-heap. For each sift-down step, compare the current candidate with the left child, then with the right child when it exists. Return the total comparison count from building the heap (the heap itself is not returned).

Hint

Count one comparison per child examined, even when the child is not smaller.

Test cases

case 1
in: [[5,4,3,2,1]]
expected: 6
case 2
in: [[1,2,3]]
expected: 2
case 3
in: [[1]]
expected: 0
case 4
in: [[]]
expected: 0
case 5
in: [[2,1,4,3,6,5]]
expected: 7
Starter codePython
def heap_build_comparisons(nums):
    # Your code here
    pass

5 test cases · run your code against them in the editor