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 mostk elements.
Input: nums =[1,2,3], k =2Output: 20Explanation:
The subarrays of `nums`with at most 2 elements are:**Subarray**| Minimum | Maximum | Sum
---|---|---|---`[1]`|1|1|2`[2]`|2|2|4`[3]`|3|3|6`[1, 2]`|1|2|3`[2, 3]`|2|3|5**Final Total**|||20The output would be 20.
Input: nums =[1,-3,1], k =2Output: -6Explanation:
The subarrays of `nums`with at most 2 elements are:**Subarray**| Minimum | Maximum | Sum
---|---|---|---`[1]`|1|1|2`[-3]`|-3|-3|-6`[1]`|1|1|2`[1, -3]`|-3|1|-2`[-3, 1]`|-3|1|-2**Final Total**|||-6The output would be -6.
For each subarray of length at most k, we want to efficiently find the minimum and maximum. We can use two monotonic queues (deques): one for minimums and one for maximums, to maintain the current window’s min and max in O(1) time per step. We slide a window of size up to k over the array and sum the min and max for all possible subarrays.
classSolution {
public:longlong sumOfMaxAndMin(vector<int>& nums, int k) {
int n = nums.size();
longlong ans =0;
for (int len =1; len <= k; ++len) {
deque<int> minq, maxq;
for (int i =0; i < n; ++i) {
while (!minq.empty() && minq.front() <= i - len) minq.pop_front();
while (!maxq.empty() && maxq.front() <= i - len) maxq.pop_front();
while (!minq.empty() && nums[minq.back()] >= nums[i]) minq.pop_back();
while (!maxq.empty() && nums[maxq.back()] <= nums[i]) maxq.pop_back();
minq.push_back(i);
maxq.push_back(i);
if (i >= len -1) {
ans += nums[minq.front()] + nums[maxq.front()];
}
}
}
return ans;
}
};
classSolution {
publiclongsumOfMaxAndMin(int[] nums, int k) {
int n = nums.length;
long ans = 0;
for (int len = 1; len <= k; ++len) {
Deque<Integer> minq =new ArrayDeque<>(), maxq =new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
while (!minq.isEmpty() && minq.peekFirst() <= i - len) minq.pollFirst();
while (!maxq.isEmpty() && maxq.peekFirst() <= i - len) maxq.pollFirst();
while (!minq.isEmpty() && nums[minq.peekLast()]>= nums[i]) minq.pollLast();
while (!maxq.isEmpty() && nums[maxq.peekLast()]<= nums[i]) maxq.pollLast();
minq.addLast(i);
maxq.addLast(i);
if (i >= len - 1) {
ans += nums[minq.peekFirst()]+ nums[maxq.peekFirst()];
}
}
}
return ans;
}
}
classSolution {
funsumOfMaxAndMin(nums: IntArray, k: Int): Long {
val n = nums.size
var ans = 0Lfor (len in1..k) {
val minq = ArrayDeque<Int>()
val maxq = ArrayDeque<Int>()
for (i in0 until n) {
while (minq.isNotEmpty() && minq.first() <= i - len) minq.removeFirst()
while (maxq.isNotEmpty() && maxq.first() <= i - len) maxq.removeFirst()
while (minq.isNotEmpty() && nums[minq.last()] >= nums[i]) minq.removeLast()
while (maxq.isNotEmpty() && nums[maxq.last()] <= nums[i]) maxq.removeLast()
minq.addLast(i)
maxq.addLast(i)
if (i >= len - 1) {
ans += nums[minq.first()] + nums[maxq.first()]
}
}
}
return ans
}
}
classSolution:
defsumOfMaxAndMin(self, nums: list[int], k: int) -> int:
from collections import deque
n = len(nums)
ans =0for l in range(1, k+1):
minq, maxq = deque(), deque()
for i in range(n):
while minq and minq[0] <= i-l:
minq.popleft()
while maxq and maxq[0] <= i-l:
maxq.popleft()
while minq and nums[minq[-1]] >= nums[i]:
minq.pop()
while maxq and nums[maxq[-1]] <= nums[i]:
maxq.pop()
minq.append(i)
maxq.append(i)
if i >= l-1:
ans += nums[minq[0]] + nums[maxq[0]]
return ans