907. Sum of Subarray Minimums - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Description:
Given an array of integers arr, find the sum of the minimum value of every (contiguous) subarray of arr. Since the answer can be very large, return it modulo (10^9 + 7).

Examples:

  1. Example 1:

    • Input: arr = [3,1,2,4]
    • Output: 17
    • Explanation:
      The subarrays of [3,1,2,4] and their minimums are:
      • [3] → 3
      • [3,1] → 1
      • [3,1,2] → 1
      • [3,1,2,4] → 1
      • [1] → 1
      • [1,2] → 1
      • [1,2,4] → 1
      • [2] → 2
      • [2,4] → 2
      • [4] → 4
        Sum = 3 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 4 = 17
  2. Example 2:

    • Input: arr = [11,81,94,43,3]
    • Output: 444
    • Explanation:
      All possible subarrays contribute to the final sum based on their minimum values.

Constraints:

  • (1 \leq \text{arr.length} \leq 3 \times 10^4)
  • (1 \leq \text{arr}[i] \leq 3 \times 10^4)

Hints Before the Solution

  1. Brute Force Idea:
    You could generate every subarray and compute its minimum, but this approach has an O(n²) or worse time complexity and will be too slow for larger inputs.

  2. Observation for Optimality:
    Instead of checking every subarray, think about the contribution of each element in arr to the final sum. If you can count how many subarrays have arr[i] as the minimum, you can multiply arr[i] by that count and add it to the sum.

  3. Monotonic Stack:
    A monotonic (increasing) stack is a useful tool to determine, for each element, the span of subarrays for which it is the minimum. In particular, calculate:

    • Left Span: How many contiguous subarrays ending at i (extending to the left) where arr[i] is the smallest.
    • Right Span: How many contiguous subarrays starting at i (extending to the right) where arr[i] remains the smallest.

Approaches

Approach 1: Brute Force (For Understanding Only)

Idea:

  • Iterate over all possible subarrays.
  • For each subarray, compute the minimum element.
  • Sum all the minimums.

Issues:

  • The time complexity is O(n²) (or worse) which is impractical given the constraints.

Approach 2: Optimal Monotonic Stack Method

Idea:
For each element arr[i], determine the number of subarrays in which it is the minimum by using the following two concepts:

  1. Previous Less Element:
    Find the distance from i back to the previous index where the element is strictly less than arr[i]. Let this distance be left[i] (if there is no such element, use i + 1).

  2. Next Less Element:
    Find the distance from i forward to the next index where the element is less than or equal to arr[i] (to handle duplicate values correctly). Let this distance be right[i] (if there is no such element, use n - i).

Contribution of arr[i]:
Each subarray in which arr[i] is the minimum can be formed by choosing any of the left[i] choices on the left and any of the right[i] choices on the right. Therefore, the total contribution of arr[i] is:
[ \text{contribution} = \text{arr}[i] \times \text{left}[i] \times \text{right}[i] ]

Why It Works:

  • By summing up the contributions from all indices, you count the minimum for every subarray exactly once.
  • Using a monotonic stack, you can compute the spans in O(n) time.

Code Implementations

Python Implementation

Python3
Python3

. . . .

Java Implementation

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:
    Both the left and right span computations are done in O(n) time using a monotonic stack. The final summing step is also O(n). Thus, the overall time complexity is O(n).

  • Space Complexity:
    O(n) space is required for the left/right arrays and the stack.

Step-by-Step Walkthrough with Visual Example

Consider arr = [3, 1, 2, 4]:

  1. Left Calculation:

    • For index 0 (value 3):
      No previous elements → left[0] = 1.
    • For index 1 (value 1):
      3 (at index 0) > 1, so left[1] = 1 (from itself) + left[0] = 2.
    • For index 2 (value 2):
      1 (at index 1) is not greater than 2 → left[2] = 1.
    • For index 3 (value 4):
      2 (at index 2) is less than 4 → left[3] = 1.
  2. Right Calculation (scanning from right):

    • For index 3 (value 4):
      No elements to the right → right[3] = 1.
    • For index 2 (value 2):
      4 (at index 3) is greater than 2 → right[2] = 1 + right[3] = 2.
    • For index 1 (value 1):
      Both 2 and 4 to the right are greater than 1 → right[1] = 1 + right[2] + ... = 3.
    • For index 0 (value 3):
      1 (at index 1) is less than 3 → right[0] = 1.
  3. Contribution Calculation:

    • Index 0: (3 \times 1 \times 1 = 3)
    • Index 1: (1 \times 2 \times 3 = 6)
    • Index 2: (2 \times 1 \times 2 = 4)
    • Index 3: (4 \times 1 \times 1 = 4)
    • Sum = 3 + 6 + 4 + 4 = 17

Common Mistakes

  • Incorrect Stack Conditions:
    Handling duplicates requires careful use of strict versus non-strict inequalities. For left spans, use >; for right spans, use >=.

  • Off-by-One Errors:
    Ensure that when no previous or next smaller element exists, you correctly use the index offsets (i.e., i + 1 or n - i).

  • Modulus Handling:
    Since the result can be very large, remember to take the modulo (10^9 + 7) at the appropriate steps.

Edge Cases

  • Single Element Array:
    The only subarray is the element itself.

  • All Elements Equal:
    Each subarray’s minimum is the same as any element. The stack logic must correctly count overlapping spans.

  • Strictly Increasing or Decreasing Arrays:
    The left/right arrays will be at their maximum or minimum possible values respectively.

  • Alternative Variation:
    You might be asked for the sum of subarray maximums. A similar approach with slight adjustments (reversing the inequality) can be used.

  • Related Problems for Further Practice:

    • Largest Rectangle in Histogram
    • Subarray Sum Problems
    • Range Minimum Query
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Adopting architectural layering techniques in design responses
What is the passing score for Cisco?
What are the 5 rules of data normalization?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;