Sliding Window Maximum
Explore how to apply the sliding window technique to find maximum values within a moving window across an integer array. This lesson helps you understand constraints and implement your solution to efficiently solve common interview problems.
We'll cover the following...
Statement
You are given an array of integers nums and a sliding window of size w that moves from left to right across the array, shifting one position at a time.
Your task is to find the maximum value within the current window at each step and return it.
Constraints:
nums.lengthnums[i]wnums.length
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps us to check if you’re solving the correct problem:
Find Maximum in Sliding Window
What should be the output if the following input is given?
nums = [-4, 5, 4, -4, 4, 6, 7, 20]
w = 2
[5, 4, 6, 20]
[5, 20]
[5, 5, 4, 4, 6, 7, 20]
[5, 5, 4, 6, 6]
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in main.js in the following coding playground. We have provided useful code templates in the other files that you may build on to solve this problem.
import Deque from "./deque.js";import MinHeap from "./min_heap.js";export function findMaxSlidingWindow(nums, w) {// Replace this placeholder return statement with your codereturn [];}