Tap here to switch tabs
Problem
Submissions

Problem: Sum of Subarray Ranges

med
30 min
Try to solve the Sum of Subarray Ranges problem.

Statement

Given an integer array nums, consider every non empty contiguous subarray of nums. For each such subarray, define its range as the difference between its maximum element and its minimum element.

Return the sum of these ranges over all non empty contiguous subarrays of nums.

Note: A follow up asks whether you can solve this in O(n)O(n) time.

Constraints:

  • 1≤1 \leq nums.length ≤1000\leq 1000

  • −109≤-10^9 \leq nums[i] ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sum of Subarray Ranges

med
30 min
Try to solve the Sum of Subarray Ranges problem.

Statement

Given an integer array nums, consider every non empty contiguous subarray of nums. For each such subarray, define its range as the difference between its maximum element and its minimum element.

Return the sum of these ranges over all non empty contiguous subarrays of nums.

Note: A follow up asks whether you can solve this in O(n)O(n) time.

Constraints:

  • 1≤1 \leq nums.length ≤1000\leq 1000

  • −109≤-10^9 \leq nums[i] ≤109\leq 10^9