AlgoMaster Logo

Sliding Window Maximum

Ashish

Ashish Pratap Singh

hard

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force

Intuition:

The simplest way to solve the sliding window maximum problem is to compute the maximum of each window separately. For each position in the array, consider all the elements in the sliding window starting from that position, and find the maximum element. This method checks each sliding window one by one and finds the maximum element.

Code:

2. Deque (Double-Ended Queue) Approach

Intuition:

Using a deque, we maintain the indexes of the array. The key is to maintain the elements of the deque in a monotonically decreasing order of their values. This means the max value for the window is at the front of the deque. We ensure that elements outside the current window or smaller elements than the current are removed.

Code:

3. Max-Heap Approach

Intuition:

This approach uses a max heap to store the elements of the current window. The largest element in the window is always at the top of the heap. We remove elements from the heap that are out of the window's current range.

Code: