From b53b78285d636f9972a4a2f4690bcd88f99cdf35 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Wed, 2 Aug 2023 13:40:47 -0700 Subject: [PATCH] Update README.md --- heaps/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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))`) + +
+