Problem
Ask
Submissions
Solution

Solution: Sliding Window Maximum

Statement

Naive approach

A naive approach is to slide the window over the input list and find the maximum in each window separately. We iterate over the input list, calculating the maximum element in each window linearly, and then adding it to the output list. In each subsequent iteration, we update the current window by removing the first element from the current window and adding the incoming element of the input list. Once we are done iterating the input list, we return the output list, containing the maximums of all (nw+1)(n−w+1) windows.

The time complexity of this approach is O(n×w)O(n \times w) ...

Problem
Ask
Submissions
Solution

Solution: Sliding Window Maximum

Statement

Naive approach

A naive approach is to slide the window over the input list and find the maximum in each window separately. We iterate over the input list, calculating the maximum element in each window linearly, and then adding it to the output list. In each subsequent iteration, we update the current window by removing the first element from the current window and adding the incoming element of the input list. Once we are done iterating the input list, we return the output list, containing the maximums of all (nw+1)(n−w+1) windows.

The time complexity of this approach is O(n×w)O(n \times w) ...