You are given an integer array nums of size n. You are asked to solve n queries for each integer i in the range 0 <= i < n.
To solve the ith query:
i + 1 of the array nums.Return a 0-indexed integer array ans of size n such that ans[i] is the answer to the ith query.
A subarray is a contiguous sequence of elements in an array.
Example 1:
Input: nums = [0,1,2,4] Output: [4,2,1,0] Explanation: i=0: - The subarrays of size 1 are [0], [1], [2], [4]. The minimum values are 0, 1, 2, 4. - The maximum of the minimum values is 4. i=1: - The subarrays of size 2 are [0,1], [1,2], [2,4]. The minimum values are 0, 1, 2. - The maximum of the minimum values is 2. i=2: - The subarrays of size 3 are [0,1,2], [1,2,4]. The minimum values are 0, 1. - The maximum of the minimum values is 1. i=3: - There is one subarray of size 4, which is [0,1,2,4]. The minimum value is 0. - There is only one value, so the maximum is 0.
Example 2:
Input: nums = [10,20,50,10] Output: [50,20,10,10] Explanation: i=0: - The subarrays of size 1 are [10], [20], [50], [10]. The minimum values are 10, 20, 50, 10. - The maximum of the minimum values is 50. i=1: - The subarrays of size 2 are [10,20], [20,50], [50,10]. The minimum values are 10, 20, 10. - The maximum of the minimum values is 20. i=2: - The subarrays of size 3 are [10,20,50], [20,50,10]. The minimum values are 10, 10. - The maximum of the minimum values is 10. i=3: - There is one subarray of size 4, which is [10,20,50,10]. The minimum value is 10. - There is only one value, so the maximum is 10.
Constraints:
n == nums.length1 <= n <= 1050 <= nums[i] <= 109Problem Overview: Given an integer array, compute the maximum among the minimum values of all subarrays for every possible window size from 1 to n. For example, among all subarrays of size k, find their minimum values and report the largest of those minimums. The result is an array of length n.
Approach 1: Brute Force Window Enumeration (O(n^3) time, O(1) space)
Enumerate every possible subarray size k. For each size, slide a window across the array and compute the minimum value inside that window. Track the maximum among those minimums. Computing the minimum for each window requires scanning up to k elements, which makes the total complexity cubic in the worst case. This approach is useful for understanding the problem definition but becomes impractical once the array grows beyond a few thousand elements.
Approach 2: Expand Each Minimum Using Monotonic Stack (O(n) time, O(n) space)
The key observation: instead of iterating over window sizes, determine the largest window where each element acts as the minimum. Use a monotonic stack to compute the previous smaller and next smaller element for every index. These boundaries define the span where arr[i] is the smallest value. If the distance between those boundaries is len, then arr[i] can serve as the minimum for a window of size len. Update result[len] with max(result[len], arr[i]). After processing all elements, propagate values backward so smaller windows inherit the best values from larger windows.
This approach converts a window enumeration problem into a boundary discovery problem. The stack maintains indices in increasing order of values while scanning the array. Each element is pushed and popped at most once, giving linear time complexity. The method relies heavily on patterns common in stack and array problems, particularly those involving nearest smaller elements.
Recommended for interviews: Interviewers expect the monotonic stack solution. A brute force explanation shows you understand the problem definition, but the optimal O(n) approach demonstrates familiarity with nearest-smaller-element patterns and span calculations, which frequently appear in advanced array and stack questions.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Window Enumeration | O(n^3) | O(1) | Small arrays or when demonstrating the problem logic before optimizing |
| Monotonic Stack with Span Calculation | O(n) | O(n) | General case and interview settings; handles large arrays efficiently |
Maximum Subarray - Amazon Coding Interview Question - Leetcode 53 - Python • NeetCode • 605,141 views views
Watch 9 more video solutions →Practice Maximum of Minimum Values in All Subarrays with our built-in code editor and test cases.
Practice on FleetCode