Time complexity to get min elements from max-heap

后端 未结 6 2357
渐次进展
渐次进展 2020-12-28 18:46

I was asked in an interview:

What is the best time complexity in getting the min element(s) from a max-heap?

I replied as O(1) a

6条回答
  •  独厮守ぢ
    2020-12-28 19:42

    Min element from Max heap :

    1. search at last level = O(n/2)= O(n)

    2. replace searched element with last element and decrease heap size by 1 = O(1)

    3. Apply Maxheapify on replaced element = O(log n)

    Total Time = O(n)+O(1)+O(log n) = O(n)

提交回复
热议问题