Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Smallest Range Covering Elements from K Lists

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "632" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists. We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c. Example 1: Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]] Output: [20,24] Explanation: List 1: [4, 10, 15, 24,26], 24 is in range [20,24]. List 2: [0, 9, 12, 20], 20 is in range [20,24]. List 3: [5, 18, 22, 30], 22 is in range [20,24]. Example 2: Input: nums = [[1,2,3],[1,2,3],[1,2,3]] Output: [1,1] Constraints: nums.length == k 1 <= k <= 3500 1 <= nums[i].length <= 50 -105 <= nums[i][j] <= 105 nums[i] is sorted in non-decreasing order.

Explanation

Here's the solution to find the smallest range covering elements from k sorted lists:

  • High-level Approach:

    • Use a min-heap to track the smallest element from each list.
    • Continuously extract the minimum element from the heap, update the range if necessary, and add the next element from the corresponding list to the heap.
    • The algorithm terminates when any of the lists is exhausted.
  • Complexity:

    • Runtime: O(n k log k), where n is the average length of the lists and k is the number of lists.
    • Storage: O(k)

Code

    import heapq

def smallestRange(nums):
    """
    Finds the smallest range that includes at least one number from each of the k lists.

    Args:
        nums: A list of k lists of sorted integers in non-decreasing order.

    Returns:
        A list representing the smallest range [start, end].
    """

    k = len(nums)
    heap = []
    max_val = float('-inf')

    # Initialize the heap with the first element from each list
    for i in range(k):
        heapq.heappush(heap, (nums[i][0], i, 0))  # (value, list_index, element_index)
        max_val = max(max_val, nums[i][0])

    range_start = float('-inf')
    range_end = float('inf')

    while True:
        min_val, list_index, element_index = heapq.heappop(heap)

        # Update the range if the current range is smaller
        if max_val - min_val < range_end - range_start:
            range_start = min_val
            range_end = max_val

        # If the list is exhausted, we're done
        if element_index + 1 == len(nums[list_index]):
            break

        # Add the next element from the list to the heap
        next_val = nums[list_index][element_index + 1]
        heapq.heappush(heap, (next_val, list_index, element_index + 1))
        max_val = max(max_val, next_val)

    return [range_start, range_end]

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Smallest Range Covering Elements from K Lists