Maximum and Minimum Sums of at Most Size K Subarrays - Practice Coding | SlaveCode
0
0123456789
0
0123456789
:
0
0123456789
0
0123456789
3430. Maximum and Minimum Sums of at Most Size K Subarrays
Hard
50 Points
Array
Math
Stack
Monotonic Stack
You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all subarrays with at most k elements.
Examples
Example 1
Input: nums = [1,2,3], k = 2
Output: 20
Explanation:
The subarrays of nums with at most 2 elements are:
The output would be 20.
Example 2
Input: nums = [1,-3,1], k = 2
Output: -6
Explanation:
The subarrays of nums with at most 2 elements are:
The output would be -6.
Constraints
1 <= nums.length <= 80000
1 <= k <= nums.length
-106 <= nums[i] <= 106
3430. Maximum and Minimum Sums of at Most Size K Subarrays
Hard
50 Points
Array
Math
Stack
Monotonic Stack
You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all subarrays with at most k elements.
Examples
Example 1
Input: nums = [1,2,3], k = 2
Output: 20
Explanation:
The subarrays of nums with at most 2 elements are:
The output would be 20.
Example 2
Input: nums = [1,-3,1], k = 2
Output: -6
Explanation:
The subarrays of nums with at most 2 elements are:
The output would be -6.