0% completed
Solution: Kth Smallest Number
Problem Statement
Given an unsorted array of numbers, find Kth smallest number in it.
Please note that it is the Kth smallest number in the sorted order, not the Kth distinct element.
Note: For a detailed discussion about different approaches to solve this problem, take a look at Kth Smallest Number.
Example 1:
Input: [1, 5, 12, 2, 11, 5], K = 3
Output: 5
Explanation: The 3rd smallest number is '5', as the first two smaller numbers are [1, 2].
Example 2:
.....
.....
.....
First
· 4 years ago
// if heap has more than 'k' numbers, remove one number if (this.minHeap.length > this.k) { this.minHeap.pop(); } How does this loop enough times to pop the minHeap if this is an if statement?
Utkarsh Gupta
· 3 years ago
Time Complexity of alternative approach would be O(N log(N)). Insertion takes O(logN) time. In the alternate approach we are first inserting all the numbers into the heap which would result in O(Nlog(n)).
Baraa Attabbaa
· 3 years ago
Initializing the min-heap with all numbers will take O(N)