LeetCode 1950. Maximum of Minimum Values in All Subarrays

原题链接在这里:https://leetcode.com/problems/maximum-of-minimum-values-in-all-subarrays/

题目:

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:

  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. 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 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.length
  • 1 <= n <= 105
  • 0 <= nums[i] <= 109

题解:

Maintain a increasing monotonic stack.

When it comes to a smaller element nums[i]. The popped out nums[j] should be largest for subarray between i - 1 - stk.peek().

At the end, pop up all the element in the stack.

There may be some gap in the result. Fill the gap from right to left. The maimum of subarray length of i must be a candidate of subarray of length i - 1.

Time Complexity: o(n). n = nums.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] findMaximums(int[] nums) {
 3         int n = nums.length;
 4         int [] res = new int[n];
 5         Stack<Integer> stk = new Stack<>();
 6         for(int i = 0; i <= n; i++){
 7             while(!stk.isEmpty() && (i == n || nums[stk.peek()] >= nums[i])){
 8                 int j = stk.pop();
 9                 int k = i - 1;
10                 if(!stk.isEmpty()){
11                     k -= stk.peek() + 1;
12                 }
13                 
14                 res[k] = Math.max(res[k], nums[j]);
15             }
16             
17             stk.push(i);
18         }
19         
20         for(int i = n - 1; i > 0; i--){
21             res[i - 1] = Math.max(res[i - 1], res[i]);
22         }
23         
24         return res;
25     }
26 }

 

posted @ 2022-08-04 14:34  Dylan_Java_NYC  阅读(107)  评论(0编辑  收藏  举报