Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Sliding Window Median

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "480" 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

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values. For examples, if arr = [2,3,4], the median is 3. For examples, if arr = [1,2,3,4], the median is (2 + 3) / 2 = 2.5. You are given an integer array nums and an integer k. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the median array for each window in the original array. Answers within 10-5 of the actual value will be accepted. Example 1: Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000] Explanation: Window position Median --------------- ----- [1 3 -1] -3 5 3 6 7 1 1 [3 -1 -3] 5 3 6 7 -1 1 3 [-1 -3 5] 3 6 7 -1 1 3 -1 [-3 5 3] 6 7 3 1 3 -1 -3 [5 3 6] 7 5 1 3 -1 -3 5 [3 6 7] 6 Example 2: Input: nums = [1,2,3,4,2,3,1,4,2], k = 3 Output: [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000] Constraints: 1 <= k <= nums.length <= 105 -231 <= nums[i] <= 231 - 1

Explanation

Here's a solution to the sliding window median problem:

  • Maintain Sorted Window: Use a sorted list (or a data structure that allows efficient insertion, deletion, and finding the median) to represent the current window.
  • Sliding Window Operations: For each window position, remove the element that's leaving the window and add the new element entering the window, maintaining the sorted order.
  • Calculate Median: Efficiently calculate the median from the sorted window.

  • Runtime Complexity: O(n*k) due to insertion and removal in a sorted list for each element. However, we can achieve O(n log k) if we use binary search to find the insertion point. Storage Complexity: O(k) to store the window.

Code

    def medianSlidingWindow(nums, k):
    import bisect

    result = []
    window = sorted(nums[:k])

    for i in range(len(nums) - k + 1):
        if k % 2 == 0:
            median = (window[k // 2 - 1] + window[k // 2]) / 2
        else:
            median = window[k // 2]
        result.append(median)

        if i < len(nums) - k:
            # Remove the outgoing element
            window.remove(nums[i])

            # Insert the incoming element
            bisect.insort(window, nums[i + k])

    return result

More from this blog

C

Chatmagic blog

2894 posts