Maximum of Minimums for Every Window Size
Maximum of Minimums for Every Window Size is a GFG problem (Hard). For every window size k from 1 to n, take the minimum of each contiguous subarray of length k, then report the largest of those minimums. Return the n answers, the one for size 1 first.
- With n up to 10⁵, solving each size on its own costs at least O(n²) in total, which is too slow.
- All values are positive, so 0 works as an empty answer slot.
- 1 <= arr.size() <= 10⁵
- 1 <= arr[i] <= 10⁶
Intuition
Instead of asking every window for its minimum, ask every element: what is the widest window in which I am the minimum?
- Its reach:
arr[j]is the minimum of any window that stays strictly between the nearest smaller value on its left and the nearest smaller value on its right. If those sit atleftandright, the widest such window has lengthright − left − 1. - Smaller sizes inherit it: any window of size
w + 1contains a window of sizewwhose minimum is at least as large, so the answers never increase as the size grows.
So record each element at its widest size, then carry the best values down to smaller sizes.
"For each element, how far does it stay the minimum (or maximum)?" is the nearest-smaller-element pattern solved with a monotonic stack. Largest Rectangle in Histogram and Sum of Subarray Minimums use the same bounds.
Approach
Before reading on, pick one element and work out the widest window in which it is the minimum. Which two neighbours decide that width? Then decide how one element's answer helps window sizes smaller than its widest one. Aim for O(n).
Keep a stack of increasing values
Scan i from 0 to n and keep a stack of indices whose values rise from bottom to top. Before pushing i, pop every index whose value is >= arr[i], because those elements can no longer be the minimum of a window that reaches i.
Read both bounds when an index is popped
When index j is popped, arr[i] is the first value to its right that is not larger, and the index now on top of the stack is the nearest smaller value to its left (or -1 if the stack is empty). One pop gives both bounds, so no second pass is needed.
Record the element at its widest size
The window between those bounds has size = i − left − 1, and arr[j] is its minimum. Set res[size] = max(res[size], arr[j]). Treating i == n as smaller than everything pops the indices left at the end, with the array's end as their right bound.
Fill the sizes nobody recorded
Some sizes get no element directly. Sweep size from n − 1 down to 1 and set res[size] = max(res[size], res[size + 1]). This is safe because the answer for a smaller window is never below the answer for a larger one.
Code in Python, C++ and Java
Common pitfalls
Skipping the backward fill
return res[1:] # right after the stack loop
for size in range(n - 1, 0, -1):
res[size] = max(res[size], res[size + 1])
return res[1:]No element is the minimum of a widest window of every size. For [10, 20, 30, 50, 10, 70, 30] nothing lands on sizes 5 and 6, so the result reads [70, 30, 20, 10, 0, 0, 10] instead of [70, 30, 20, 10, 10, 10, 10].
Stopping at equal values on both sides
# two-pass version, both passes while stack and arr[stack[-1]] > arr[i]:
# at least one pass pops equal values while stack and arr[stack[-1]] >= arr[i]:
Many solutions find the left and right bounds in two separate passes. If both passes treat an equal value as a wall, a run of equal values is cut into pieces and its full width is never recorded: [1, 1] returns [1, 0]. The one-pass code above is safe either way, because the last equal value popped still sees the full width.
Edge cases
It is popped when i == n with the empty stack as its left bound, so its size is 1 and the answer is that element.
For [5, 5, 5] each push pops the previous equal value. The last one is popped at the end with size 3, and the fill copies 5 to every size.
Complexity
Maximum of Minimums for Every Window Size FAQ
Why not run a sliding window minimum for every size?
A deque finds all minimums for one window size in O(n), so all n sizes take O(n²) time. At n = 10⁵ that is about 10¹⁰ steps, far too slow. The stack handles every size in one pass.
LeetCode 1950 looks the same. Is it?
Yes. Maximum of Minimum Values in All Subarrays (LeetCode 1950, premium) asks for the same n answers, using query index i for window size i + 1. The same code works unchanged.
Does the answer for size 1 always equal the largest element?
Yes, and the answer for size n is always the smallest element. A window of size 1 is a single element, so its minimum is the element itself; the only window of size n is the whole array. Both make quick checks for your output.