Maximum of Minimums for Every Window Size
Problem Description
Given an array of n integers, return an array result of length n where result[i] is the maximum of minimums across all subarrays of length i+1.
Example:
Input: arr = [10,20,30,50,10,70,30]
Output: [70,30,20,10,10,10,10]
Explanation:
- Window size 1: max of each element = 70
- Window size 2: windows [10,20],[20,30],[30,50],[50,10],[10,70],[70,30] → mins [10,20,30,10,10,30] → max=30
- Window size 3: max of window-mins = 20
- ... and so on
DE Shaw Tier 1 — classic GfG problem.
Optimal: Monotonic stack O(n). For each element, find the span of windows where it is the minimum (using previous-smaller and next-smaller arrays). Update result for that window size. Then propagate: result[i] = max(result[i], result[i+1]) right to left.
Example Test Cases
Example 1
Input: [[10,20,30,50,10,70,30]]
Expected: [70,30,20,10,10,10,10]
Example 2
Input: [[10,20,30]]
Expected: [30,20,10]
Example 3
Input: [[5]]
Expected: [5]
Run your code to see the results