1
00:00:00,000 --> 00:00:04,640
A min-heap exposes its smallest value at
the root while preserving a partial

2
00:00:04,720 --> 00:00:06,720
ordering among all other values.

3
00:00:07,120 --> 00:00:10,080
Python's heapq operates on an ordinary
list.

4
00:00:10,480 --> 00:00:13,840
Our example keeps the three largest scores
seen so far.

5
00:00:14,240 --> 00:00:18,560
The smallest retained score is the
threshold a new score must beat, so a

6
00:00:18,680 --> 00:00:22,480
min-heap is useful even though the
requested answer contains the largest

7
00:00:22,760 --> 00:00:23,240
values.

8
00:00:23,440 --> 00:00:26,240
The final output will require a separate
sort.

9
00:00:26,567 --> 00:00:29,447
A new score is compared with the heap
root.

10
00:00:29,487 --> 00:00:33,100
The heap root is the smallest score
currently retained.

11
00:00:33,234 --> 00:00:36,140
Keep or replace according to that
comparison.

12
00:00:36,194 --> 00:00:40,687
A score no larger than the root cannot
improve the largest-k collection.

13
00:00:40,727 --> 00:00:45,487
A larger score replaces the root, and heap
repair restores the ordering rule.

14
00:00:45,527 --> 00:00:49,687
After each input, the heap contains the
largest k values from the processed

15
00:00:49,767 --> 00:00:53,287
prefix, with ties handled as repeated
values.

16
00:00:53,533 --> 00:00:58,053
The root is two after heapifying the first
three scores, six,

17
00:00:58,093 --> 00:00:59,613
two, nine.

18
00:00:59,693 --> 00:01:04,306
Its children are six and nine, both at
least as large as their parent.

19
00:01:04,360 --> 00:01:09,053
The child indices of a node at index i are
two i plus one and two i

20
00:01:09,133 --> 00:01:10,226
plus two.

21
00:01:10,280 --> 00:01:13,893
The rule does not sort siblings or
unrelated branches.

22
00:01:13,933 --> 00:01:18,573
Although this three-value list happens to
look sorted, a heap in general is not a

23
00:01:18,773 --> 00:01:19,933
sorted list.

24
00:01:20,167 --> 00:01:25,167
Heapify the first three scores to obtain
the heap two, six, nine.

25
00:01:25,207 --> 00:01:29,167
Read four, which exceeds the root two, and
replace two.

26
00:01:29,207 --> 00:01:31,967
The heap becomes four, six, nine.

27
00:01:32,007 --> 00:01:35,407
Read eight, which exceeds four, and
replace four.

28
00:01:35,447 --> 00:01:38,220
The heap becomes six, eight, nine.

29
00:01:38,274 --> 00:01:42,487
At each step, the discarded root is the
least useful retained score for the

30
00:01:42,607 --> 00:01:44,247
largest-three objective.

31
00:01:44,467 --> 00:01:47,987
The retained values are six, eight, and
nine.

32
00:01:48,027 --> 00:01:52,987
The required result is descending, so sort
those three values to return nine,

33
00:01:53,027 --> 00:01:54,494
eight, six.

34
00:01:54,600 --> 00:01:58,867
Reading the heap's internal list directly
does not promise descending or ascending

35
00:01:58,947 --> 00:01:59,880
output.

36
00:01:59,934 --> 00:02:04,387
If the question only asks for the kth
largest value, the retained minimum at the

37
00:02:04,467 --> 00:02:08,627
root already supplies that value and a
final sort is unnecessary for that

38
00:02:08,787 --> 00:02:10,387
different output contract.

39
00:02:10,633 --> 00:02:15,433
Check the requested k against the input
length and return an empty list when k

40
00:02:15,513 --> 00:02:16,593
is zero.

41
00:02:16,633 --> 00:02:20,193
Copy the first k values and heapify that
copy.

42
00:02:20,233 --> 00:02:23,006
Compare each remaining value with the
root.

43
00:02:23,140 --> 00:02:26,206
Replace the root only when the candidate
is larger.

44
00:02:26,260 --> 00:02:31,033
This condition matters because heapreplace
always removes the old root, even when its

45
00:02:31,113 --> 00:02:32,673
new value is smaller.

46
00:02:32,713 --> 00:02:36,313
Finally return the retained values in
descending order.

47
00:02:36,567 --> 00:02:40,567
Heapify builds a heap from a complete list
in linear time.

48
00:02:40,647 --> 00:02:45,647
Heappush adds one value and repairs the
path to preserve the invariant.

49
00:02:45,687 --> 00:02:49,167
Heappop removes the minimum and repairs
the heap.

50
00:02:49,207 --> 00:02:53,607
Those single updates take logarithmic
amortized time in heap size, while

51
00:02:53,727 --> 00:02:56,727
inspecting index zero takes constant time.

52
00:02:56,807 --> 00:02:59,367
Heapreplace keeps the size fixed.

53
00:02:59,447 --> 00:03:03,927
Never use an arbitrary list pop to remove
the root and expect the heap invariant to

54
00:03:04,007 --> 00:03:04,967
survive.

55
00:03:05,200 --> 00:03:09,600
For positive k, initialization takes
linear time in k.

56
00:03:09,720 --> 00:03:13,440
Each of the remaining values can trigger a
logarithmic replacement.

57
00:03:13,520 --> 00:03:16,880
Sorting the retained answer costs k log k.

58
00:03:16,920 --> 00:03:21,440
A compact bound for this implementation is
big O of n log of k plus

59
00:03:21,520 --> 00:03:22,107
one.

60
00:03:22,133 --> 00:03:26,120
The plus one keeps the expression
meaningful for k equal to one.

61
00:03:26,160 --> 00:03:30,853
Auxiliary space is big O of k for the heap
and final sort workspace.

62
00:03:30,907 --> 00:03:34,720
For zero k, the function returns an empty
list immediately.

63
00:03:34,967 --> 00:03:38,967
Test k equal to zero, one, and the input
length.

64
00:03:39,207 --> 00:03:43,607
Repeated values must remain eligible
because this problem preserves ties as

65
00:03:43,687 --> 00:03:45,020
separate scores.

66
00:03:45,074 --> 00:03:47,647
An out-of-range k is rejected.

67
00:03:47,687 --> 00:03:52,527
The interview mistake is to treat the
heap's internal list as sorted output.

68
00:03:52,567 --> 00:03:57,447
Explain the retained-set invariant, the
role of its smallest member, and the cost

69
00:03:57,527 --> 00:03:59,367
of the final output ordering.
