Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1950: Maximum of Minimum Values in All Subarrays

In this guide, we solve Leetcode #1950 Maximum of Minimum Values in All Subarrays in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Stack, Array, Monotonic Stack

Intuition

We need the next greater or smaller element efficiently, which is exactly what a monotonic stack offers.

Each element is pushed and popped at most once, yielding a linear-time scan.

Approach

Maintain a stack that is either increasing or decreasing, depending on the query.

When the invariant is broken, pop and resolve answers for those indices.

Steps:

  • Scan elements once.
  • Pop while the monotonic condition is violated.
  • Use stack indices to update answers.

Example

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.

Python Solution

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

Complexity

The time complexity is O(n). The space complexity is O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy