LeetCode 480: Sliding Window Median Solution
Master LeetCode problem 480 (Sliding Window Median), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
480. Sliding Window Median
Problem Explanation
Explanation
To solve this problem, we can use two heaps - a max heap to store the left half of the window elements and a min heap to store the right half of the window elements. By maintaining these two heaps, we can efficiently find the median of the current window.
Here is the step-by-step algorithm:
- Initialize two heaps: a max heap
leftto store the left half of the window elements and a min heaprightto store the right half of the window elements. - Iterate over the input array using a sliding window of size
k:- Add elements to the heaps based on their values.
- Balance the heaps if necessary by transferring elements between them.
- Calculate the median based on the top elements of the heaps.
- Add the median to the result array.
- Remove the element that goes out of the sliding window.
- Return the result array containing the medians of all sliding windows.
Time Complexity: O(n log k) - We iterate through all elements in the input array and perform log k operations for heap balancing in each iteration. Space Complexity: O(k) - The space used by the two heaps.
Solution Code
import java.util.*;
class Solution {
public double[] medianSlidingWindow(int[] nums, int k) {
PriorityQueue<Integer> left = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> right = new PriorityQueue<>();
List<Double> medians = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (left.size() <= right.size()) {
right.offer(nums[i]);
left.offer(right.poll());
} else {
left.offer(nums[i]);
right.offer(left.poll());
}
if (left.size() + right.size() == k) {
double median;
if (left.size() == right.size()) {
median = ((double) left.peek() + right.peek()) / 2.0;
} else {
median = (double) left.peek();
}
medians.add(median);
if (!left.remove(nums[i - k + 1])) {
right.remove(nums[i - k + 1]);
}
}
}
return medians.stream().mapToDouble(Double::doubleValue).toArray();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 480 (Sliding Window Median)?
This page provides optimized solutions for LeetCode problem 480 (Sliding Window Median) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 480 (Sliding Window Median)?
The time complexity for LeetCode 480 (Sliding Window Median) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 480 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 480 in Java, C++, or Python.