diff --git a/heaps/README.md b/heaps/README.md index 61b2495..ada487c 100644 --- a/heaps/README.md +++ b/heaps/README.md @@ -245,3 +245,30 @@ class MaxHeap: - using the heap to sort the input array * heapsort traditionally uses a max-heap to sort the array, although a min-heap also works. * this is not a stable sort. + +
+ +---- + +### top k elements problem (`O(klog(N) + N)`) + +
+ +1. construct a max (min) heap and add all elements into it (`O(N)`) +2. traverse and delete the top element, storing the value into a resulting array +3. repeat 2. until all k elements are removed (`O(k * log(N))`) + +
+ +---- + +### the kth-element problem + +
+ +1. construct a max (min) heap and add all elements into it (`O(N)`) +2. traverse and delete the top element +3. repeat 2. until k-th largest (smallest) is found (`O(k * log(N))`) + +
+