|
| 1 | +# Top-K Largest Elements in an Array |
| 2 | + |
| 3 | +Given an integer array nums, return the 3 largest elements in the array in any order. |
| 4 | + |
| 5 | +## Example |
| 6 | + |
| 7 | +```text |
| 8 | +Input: nums = [9, 3, 7, 1, -2, 6, 8] |
| 9 | +Output: [8, 7, 9] |
| 10 | +# or [7, 9, 8] or [9, 7, 8] ... |
| 11 | +``` |
| 12 | + |
| 13 | +## Solution |
| 14 | + |
| 15 | +Here's how we can solve this problem using a min-heap: |
| 16 | + |
| 17 | +- Create a min-heap that stores the first 3 elements of the array. These represent the 3 largest elements we have seen so |
| 18 | + far, with the smallest of the 3 at the root of the heap. |
| 19 | +  |
| 20 | + |
| 21 | +- Iterate through the remaining elements in the array. |
| 22 | + - If the current element is larger than the root of the heap, pop the root and push the current element into the heap. |
| 23 | + - Otherwise, continue to the next element. |
| 24 | + |
| 25 | +  |
| 26 | +  |
| 27 | +  |
| 28 | +  |
| 29 | + |
| 30 | +After iterating through all the elements, the heap contains the 3 largest elements in the array. |
| 31 | + |
| 32 | +### Complexity Analysis |
| 33 | + |
| 34 | +#### Time Complexity Breakdown |
| 35 | + |
| 36 | +- The heapify function takes O(3) = O(1) time |
| 37 | +- We iterate through all elements in the array once: O(n) time |
| 38 | +- The heappop and heappush operations take O(log 3) = O(1) time each |
| 39 | + |
| 40 | +#### Space Complexity |
| 41 | + |
| 42 | +- We use a heap of size 3 to store the 3 largest elements: O(3) = O(1) space |
| 43 | +- The algorithm uses constant space regardless of input size |
| 44 | + |
| 45 | +Note: The time and space complexity become more interesting when 3 is a variable number k. |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +# Kth Largest Element in an Array |
| 50 | + |
| 51 | +Write a function that takes an array of unsorted integers nums and an integer k, and returns the kth largest element in |
| 52 | +the array. This function should run in O(n log k) time, where n is the length of the array. |
| 53 | + |
| 54 | +## Examples |
| 55 | + |
| 56 | +```text |
| 57 | +Input: |
| 58 | +nums = [5, 3, 2, 1, 4] |
| 59 | +k = 2 |
| 60 | +
|
| 61 | +Output: 4 |
| 62 | +``` |
| 63 | + |
| 64 | +## Solutions |
| 65 | + |
| 66 | +- [Approach 1](#approach-1-sorting) |
| 67 | +- [Approach 2](#approach-2-min-heap) |
| 68 | + |
| 69 | +### Approach 1: Sorting |
| 70 | + |
| 71 | +The simplest approach is to sort the array in descending order and return the kth element. This approach has a time |
| 72 | +complexity of O(n log n) where n is the number of elements in the array, and a space complexity of O(1). |
| 73 | + |
| 74 | +### Approach 2: Min Heap |
| 75 | + |
| 76 | +By using a min-heap, we can reduce the time complexity to O(n log k), where n is the number of elements in the array and |
| 77 | +k is the value of k. |
| 78 | +The idea behind this solution is to iterate over the elements in the array while storing the k largest elements we've |
| 79 | +seen so far in a min-heap. At each element, we check if it is greater than the smallest element (the root) of the heap. |
| 80 | +If it is, we pop the smallest element from the heap and push the current element into the heap. This way, the heap will |
| 81 | +always contain the k largest elements we've seen so far. |
| 82 | +After iterating over all the elements, the root of the heap will be the kth largest element in the array. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +#### Complexity Analysis |
| 97 | + |
| 98 | +##### Time Complexity: O(n log k) |
| 99 | + |
| 100 | +Where n is the number of elements in the array and k is the input parameter. We iterate over |
| 101 | +all elements, and in the worst case, we both push and pop each element from the heap, which takes O(log k) time per |
| 102 | +element. |
| 103 | + |
| 104 | +##### Space Complexity: O(k) |
| 105 | + |
| 106 | +Where k is the input parameter. The space is used by the heap to store the k largest elements. |
0 commit comments