Start learning free

LESSON 275 OF 410 · BINARY SEARCH

Smallest K Integers V in O(n log maxVal) Time Complexity using JavaScript

Practice O(n log n) Read the full explanation

Given an array of positive integers nums, return the smallest k values, in any order you want.

Example:

Input: nums = [5, 9, 3, 6, 2, 1, 3, 2, 7, 5], k = 4
Output: [1, 2, 2, 3]
Explanation: Smallest number is 1, 2nd smallest is 2, 
            3rd smallest is 2, 4th smallest is 3
The result can be in any order, [2, 1, 3, 2] is also a correct answer.

Note:

For this lesson, your algorithm should run in O(n log V) time, where V is the range of values in nums (from the smallest to the largest value).


STEP 1 OF 21
    Ctrl + Enter to check

    JAVASCRIPT · BINARY SEARCH

    Smallest K in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to find the smallest k integers from an array of positive integers. This problem is significant in scenarios where we need to filter out the smallest elements from a large dataset, such as in data analysis or competitive programming.

    Potential pitfalls include misunderstanding the requirement to return the smallest k values in any order and not optimizing the solution to meet the time complexity constraint.

    Approach

    To solve this problem, we can consider several approaches:

    Naive Solution

    A naive solution would be to sort the array and then return the first k elements. However, this approach has a time complexity of O(n log n), which is not optimal for large datasets.

    Optimized Solution

    We can use a min-heap (priority queue) to efficiently find the smallest k elements. The min-heap allows us to maintain the smallest elements seen so far, and it provides O(log k) insertion and deletion times.

    Algorithm

    Here is a step-by-step breakdown of the optimized algorithm using a min-heap:

    1. Initialize an empty min-heap.
    2. Iterate through each element in the array.
    3. Insert each element into the min-heap.
    4. If the size of the min-heap exceeds k, remove the largest element (which is at the root of the heap).
    5. After processing all elements, the min-heap will contain the smallest k elements.
    6. Extract the elements from the min-heap to get the result.

    Code Implementation

    // Function to find the smallest k integers
    function smallestK(nums, k) {
        // Edge case: if k is 0, return an empty array
        if (k === 0) return [];
        
        // Create a min-heap using a priority queue
        const minHeap = new MinPriorityQueue();
        
        // Insert all elements into the min-heap
        for (let num of nums) {
            minHeap.enqueue(num);
        }
        
        // Extract the smallest k elements
        const result = [];
        for (let i = 0; i < k; i++) {
            result.push(minHeap.dequeue().element);
        }
        
        return result;
    }
    
    // Example usage
    const nums = [5, 9, 3, 6, 2, 1, 3, 2, 7, 5];
    const k = 4;
    console.log(smallestK(nums, k)); // Output: [1, 2, 2, 3]
    

    Complexity Analysis

    The time complexity of this approach is O(n log k) because each insertion and deletion operation in the min-heap takes O(log k) time, and we perform these operations n times. The space complexity is O(k) for storing the k smallest elements in the min-heap.

    Edge Cases

    Potential edge cases include:

    • k is 0: The function should return an empty array.
    • k is greater than the length of the array: The function should return the entire array.
    • All elements in the array are the same: The function should handle this gracefully.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple case: nums = [5, 9, 3, 6, 2, 1, 3, 2, 7, 5], k = 4
    • Edge case: nums = [1, 1, 1, 1], k = 2
    • Edge case: nums = [1, 2, 3], k = 0
    • Edge case: nums = [1, 2, 3], k = 5

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the problem requirements and constraints thoroughly.
    • Think about different data structures that can help optimize the solution.
    • Break down the problem into smaller steps and solve each step incrementally.
    • Practice solving similar problems to improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to find the smallest k integers from an array of positive integers using an optimized approach with a min-heap. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving problem-solving skills and preparing for coding interviews.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free