Maximum of Minimum Values in All Subarrays - Problem

You are given an integer array nums of size n. You need to solve n queries for each integer i in the range 0 ≤ i < n.

To solve the ith query:

  1. Find the minimum value in each possible subarray of size i + 1 of the array nums
  2. Find the maximum of those minimum values

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [7,9,8,6,2]
Output: [9,8,7,6,2]
💡 Note: Size 1: min values are [7,9,8,6,2], max = 9. Size 2: min values are [7,8,6,2], max = 8. Size 3: min values are [7,6,2], max = 7. Size 4: min values are [6,2], max = 6. Size 5: min value is [2], max = 2.
Example 2 — All Same Values
$ Input: nums = [5,5,5,5]
Output: [5,5,5,5]
💡 Note: All elements are the same, so minimum of any subarray is 5, and maximum of minimums is always 5.
Example 3 — Decreasing Order
$ Input: nums = [10,6,3,1]
Output: [10,6,3,1]
💡 Note: Size 1: max min = 10. Size 2: subarrays [10,6],[6,3],[3,1] have mins [6,3,1], max = 6. Size 3: subarrays [10,6,3],[6,3,1] have mins [3,1], max = 3. Size 4: subarray [10,6,3,1] has min = 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Maximum of Minimum Values in All Subarrays INPUT Array nums (size n=5) 7 i=0 9 i=1 8 i=2 6 i=3 2 i=4 Example Subarrays: Size 1: [7],[9],[8],[6],[2] Size 2: [7,9],[9,8],[8,6],[6,2] Size 3: [7,9,8],[9,8,6],[8,6,2] Size 4: [7,9,8,6],[9,8,6,2] Size 5: [7,9,8,6,2] For each query i: 1. Find MIN of each subarray of size (i+1) 2. Return MAX of those mins ALGORITHM STEPS 1 Find Contribution Range Use stack to find how far each element can dominate 2 Compute Window Size For each element, calculate max window where it's min 3 Map Element to Size ans[window_size] = max val that can be min for size 4 Fill Remaining Propagate values from smaller to larger sizes Calculation Example: Size 1: max(7,9,8,6,2)=9 Size 2: max(7,8,6,2)=8 Size 3: max(7,6,2)=7 Size 4: max(6,2)=6 Size 5: max(2)=2 Result: [9,8,7,6,2] FINAL RESULT Output Array ans[] 9 i=0 8 i=1 7 i=2 6 i=3 2 i=4 Answer Breakdown: ans[0]=9: Size 1 subarrays max of mins = max(7,9,8,6,2) ans[1]=8: Size 2 subarrays max of mins = max(7,8,6,2) ans[2]=7: Size 3 subarrays max of mins = max(7,6,2) ans[3]=6: Size 4 subarrays max of mins = max(6,2) ans[4]=2: Size 5 (full array) min of entire array = 2 Key Insight: Using monotonic stack, we find the range where each element is the minimum. If element nums[i] is minimum in a window of size k, then ans[k-1] = max(ans[k-1], nums[i]). This gives O(n) complexity instead of brute force O(n^3). The stack finds Previous/Next Smaller Element in linear time. TutorialsPoint - Maximum of Minimum Values in All Subarrays | Optimal Solution Time: O(n) | Space: O(n)
Asked in
Google 25 Amazon 18 Facebook 15 Microsoft 12
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen