Skip to content

Latest commit

 

History

History
75 lines (58 loc) · 3.18 KB

File metadata and controls

75 lines (58 loc) · 3.18 KB

13.1 Top 'K' Numbers (easy)

Problem Statement

Given an unsorted array of numbers, find the ‘K’ largest numbers in it.

Note: For a detailed discussion about different approaches to solve this problem, take a look at Kth Smallest Number.

Example 1:

Input: [3, 1, 5, 12, 2, 11], K = 3
Output: [5, 12, 11]

Example 2:

Input: [5, 12, 11, -1, 12], K = 3
Output: [12, 11, 12]

Approach

The approach is to use a min-heap to keep track of the top 'K' largest numbers in the array. The steps are:

  • Create a min-heap of size 'K' and insert the first 'K' numbers of the array into it.
  • Iterate through the remaining numbers of the array, and for each number, do the following:
    • If the number is larger than the root of the min-heap, remove the root and insert the number into the min-heap.
    • Otherwise, ignore the number as it is not among the top 'K' largest numbers.
  • Return the contents of the min-heap as the answer.

Solution

import java.util.*;

class Solution {

  public static List<Integer> findKLargestNumbers(int[] nums, int k) {
    PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>((n1, n2) -> n1 - n2);
    // put first 'K' numbers in the min heap
    for (int i = 0; i < k; i++)
      minHeap.add(nums[i]);

    // go through the remaining numbers of the array, if the number from the array is bigger than the
    // top (smallest) number of the min-heap, remove the top number from heap and add the number from array
    for (int i = k; i < nums.length; i++) {
      if (nums[i] > minHeap.peek()) {
        minHeap.poll();
        minHeap.add(nums[i]);
      }
    }

    // the heap has the top 'K' numbers, return them in a list
    return new ArrayList<>(minHeap);
  }
}

Complexities

The above code is an implementation of the heap sort algorithm in Java. The complexity of the code depends on the following factors:

  • The size of the input array, denoted by N
  • The number of largest elements to find, denoted by K
  • The operations performed on the min-heap, such as insertion, deletion, and peeking

The time complexity of the code can be analyzed as follows:

  • 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.
  • 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.
  • The return statement converts the min-heap into a list, which takes O(K) time.

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).

The space complexity of the code can be analyzed as follows:

  • The min-heap uses O(K) space to store the top K elements of the array.
  • The list uses O(K) space to store the same elements as the heap.
  • The rest of the variables use O(1) space.

Therefore, the total space complexity of the code is O(K + K + 1), which is asymptotically equivalent to O(K).