|
| 1 | +# 13.1 Top 'K' Numbers (easy) |
| 2 | +## Problem Statement |
| 3 | +Given an unsorted array of numbers, find the ‘K’ largest numbers in it. |
| 4 | + |
| 5 | +> Note: For a detailed discussion about different approaches to solve this problem, take a look at Kth Smallest Number. |
| 6 | + |
| 7 | +Example 1: |
| 8 | +``` |
| 9 | +Input: [3, 1, 5, 12, 2, 11], K = 3 |
| 10 | +Output: [5, 12, 11] |
| 11 | +``` |
| 12 | +Example 2: |
| 13 | +``` |
| 14 | +Input: [5, 12, 11, -1, 12], K = 3 |
| 15 | +Output: [12, 11, 12] |
| 16 | +``` |
| 17 | + |
| 18 | +## Approach |
| 19 | +The approach is to use a min-heap to keep track of the top 'K' largest numbers in the array. The steps are: |
| 20 | + |
| 21 | +- Create a min-heap of size 'K' and insert the first 'K' numbers of the array into it. |
| 22 | +- Iterate through the remaining numbers of the array, and for each number, do the following: |
| 23 | + - If the number is larger than the root of the min-heap, remove the root and insert the number into the min-heap. |
| 24 | + - Otherwise, ignore the number as it is not among the top 'K' largest numbers. |
| 25 | +- Return the contents of the min-heap as the answer. |
| 26 | + |
| 27 | +## Solution |
| 28 | +```java |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +class Solution { |
| 32 | + |
| 33 | + public static List<Integer> findKLargestNumbers(int[] nums, int k) { |
| 34 | + PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>((n1, n2) -> n1 - n2); |
| 35 | + // put first 'K' numbers in the min heap |
| 36 | + for (int i = 0; i < k; i++) |
| 37 | + minHeap.add(nums[i]); |
| 38 | + |
| 39 | + // go through the remaining numbers of the array, if the number from the array is bigger than the |
| 40 | + // top (smallest) number of the min-heap, remove the top number from heap and add the number from array |
| 41 | + for (int i = k; i < nums.length; i++) { |
| 42 | + if (nums[i] > minHeap.peek()) { |
| 43 | + minHeap.poll(); |
| 44 | + minHeap.add(nums[i]); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // the heap has the top 'K' numbers, return them in a list |
| 49 | + return new ArrayList<>(minHeap); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Complexities |
| 55 | +The above code is an implementation of the heap sort algorithm in Java. The complexity of the code depends on the following factors: |
| 56 | + |
| 57 | +- The size of the input array, denoted by N |
| 58 | +- The number of largest elements to find, denoted by K |
| 59 | +- The operations performed on the min-heap, such as insertion, deletion, and peeking |
| 60 | + |
| 61 | +The time complexity of the code can be analyzed as follows: |
| 62 | + |
| 63 | +- The first for loop iterates over the first K elements of the array and inserts them into the min-heap. This takes O(K * log K) time, since each insertion takes O(log K) time, where K is the size of the heap. |
| 64 | +- The second for loop iterates over the remaining N - K elements of the array and compares them with the root of the min-heap. If the element is larger than the root, it removes the root and inserts the element into the heap. This takes O((N - K) * log K) time, since each deletion and insertion takes O(log K) time. |
| 65 | +- The return statement converts the min-heap into a list, which takes O(K) time. |
| 66 | + |
| 67 | +Therefore, the total time complexity of the code is O(K * log K + (N - K) * log K + K), which is asymptotically equivalent to O(N * log K). |
| 68 | + |
| 69 | +The space complexity of the code can be analyzed as follows: |
| 70 | + |
| 71 | +- The min-heap uses O(K) space to store the top K elements of the array. |
| 72 | +- The list uses O(K) space to store the same elements as the heap. |
| 73 | +- The rest of the variables use O(1) space. |
| 74 | + |
| 75 | +Therefore, the total space complexity of the code is O(K + K + 1), which is asymptotically equivalent to O(K). |
0 commit comments