Maximum of Minimum Values in All Subarrays Problem
Description
LeetCode Problem 1950.
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 i^th query:
- Find the minimum value in each possible subarray of size
i + 1of the arraynums. - Find the maximum of those minimum values. This maximum is the answer to the query.
Return a 0-indexed integer array ans of size n such that ans[i] is the answer to the i^th 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 <= 10^50 <= nums[i] <= 10^9
Solution
Approach: Monotonic Stack
For each element nums[i], find how far it is the minimum: let left be the index of the previous element smaller than it and right the index of the next element smaller than it (using -1 and n when none exists), computed with two monotonic stack passes. Then nums[i] is the minimum of some subarray of length m = right - left - 1, and of no longer one.
So ans[m - 1] is at least nums[i]; we keep the maximum over all i with that m. A value that is the minimum of a window of length m is also the minimum of some window of every shorter length (take a sub-window containing it), so finally we sweep from the right and set ans[i] = max(ans[i], ans[i + 1]).
Complexity: O(n) time and O(n) space.
class Solution:
def findMaximums(self, nums: List[int]) -> List[int]:
n = len(nums)
left = [-1] * n
right = [n] * n
stk = []
for i, x in enumerate(nums):
while stk and nums[stk[-1]] >= x:
stk.pop()
if stk:
left[i] = stk[-1]
stk.append(i)
stk = []
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] >= nums[i]:
stk.pop()
if stk:
right[i] = stk[-1]
stk.append(i)
ans = [0] * n
for i in range(n):
m = right[i] - left[i] - 1
ans[m - 1] = max(ans[m - 1], nums[i])
for i in range(n - 2, -1, -1):
ans[i] = max(ans[i], ans[i + 1])
return ans