LeetCode 1950: Maximum of Minimum Values in All Subarrays Solution

Master LeetCode problem 1950 (Maximum of Minimum Values in All Subarrays), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1950. Maximum of Minimum Values in All Subarrays

Problem Explanation

Explanation:

This problem can be solved using a stack to efficiently calculate the maximum of minimum values in all subarrays. The key idea is to find the left and right closest smaller elements for each element in the array, which helps in determining the maximum subarray length with that element as the minimum.

  1. Iterate through the array from left to right to find the left closest smaller element for each element.
  2. Iterate through the array from right to left to find the right closest smaller element for each element.
  3. Calculate the subarray length for each element as the minimum by subtracting the indices of the left and right closest smaller elements.
  4. Update the maximum result by taking the maximum of the minimum values in all subarrays.

By following the above approach, we can efficiently find the maximum of minimum values in all subarrays.

Time Complexity: O(N) Space Complexity: O(N)

:

Solution Code

public int[] maxOfMinValues(int[] A) {
    int n = A.length;
    int[] left = new int[n];
    int[] right = new int[n];
    Stack<Integer> stack = new Stack<>();
    
    for (int i = 0; i < n; i++) {
        while (!stack.isEmpty() && A[stack.peek()] >= A[i]) {
            stack.pop();
        }
        left[i] = stack.isEmpty() ? -1 : stack.peek();
        stack.push(i);
    }
    
    stack.clear();
    
    for (int i = n - 1; i >= 0; i--) {
        while (!stack.isEmpty() && A[stack.peek()] >= A[i]) {
            stack.pop();
        }
        right[i] = stack.isEmpty() ? n : stack.peek();
        stack.push(i);
    }
    
    int[] result = new int[n];
    Arrays.fill(result, 0);
    
    for (int i = 0; i < n; i++) {
        int len = right[i] - left[i] - 1;
        result[len - 1] = Math.max(result[len - 1], A[i]);
    }
    
    for (int i = n - 2; i >= 0; i--) {
        result[i] = Math.max(result[i], result[i + 1]);
    }
    
    return result;
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1950 (Maximum of Minimum Values in All Subarrays)?

This page provides optimized solutions for LeetCode problem 1950 (Maximum of Minimum Values in All Subarrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1950 (Maximum of Minimum Values in All Subarrays)?

The time complexity for LeetCode 1950 (Maximum of Minimum Values in All Subarrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1950 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1950 in Java, C++, or Python.

Back to LeetCode Solutions