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 SubarraysInput: [7, 9, 8, 6, 2]79862Size 1 subarrays:[7], [9], [8], [6], [2] → mins: 7,9,8,6,2 → max: 9Size 2 subarrays:[7,9], [9,8], [8,6], [6,2] → mins: 7,8,6,2 → max: 8Size 3 subarrays:[7,9,8], [9,8,6], [8,6,2] → mins: 7,6,2 → max: 7Size 4 subarrays:[7,9,8,6], [9,8,6,2] → mins: 6,2 → max: 6Size 5 subarrays:[7,9,8,6,2] → mins: 2 → max: 2Output: [9, 8, 7, 6, 2]
Understanding the Visualization
1
Input
Array [7,9,8,6,2] with 5 elements
2
Process
For each size, find minimums of all subarrays, then take maximum
3
Output
Array [9,8,7,6,2] representing max of mins for each size
Key Takeaway
🎯 Key Insight: Each element dominates as minimum within a specific range of subarray sizes
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