Description
Count Non-Decreasing Subarrays After K Operations
HardYou are given an array nums of n integers and an integer k.
For each subarray of nums, you can apply up to k operations on it. In each operation, you increment any element of the subarray by 1.
Note that each subarray is considered independently, meaning changes made to one subarray do not persist to another.
Return the number of subarrays that you can make non-decreasing after performing at most k operations.
An array is said to be non-decreasing if each element is greater than or equal to its previous element, if it exists.
Example 1:
Input: nums = [6,3,1,2,4,4], k = 7
Output: 17
Explanation:
Out of all 21 possible subarrays of nums, only the subarrays [6, 3, 1], [6, 3, 1, 2], [6, 3, 1, 2, 4] and [6, 3, 1, 2, 4, 4] cannot be made non-decreasing after applying up to k = 7 operations. Thus, the number of non-decreasing subarrays is 21 - 4 = 17.
Example 2:
Input: nums = [6,3,1,3,6], k = 4
Output: 12
Explanation:
The subarray [3, 1, 3, 6] along with all subarrays of nums with three or fewer elements, except [6, 3, 1], can be made non-decreasing after k operations. There are 5 subarrays of a single element, 4 subarrays of two elements, and 2 subarrays of three elements except [6, 3, 1], so there are 1 + 5 + 4 + 2 = 12 subarrays that can be made non-decreasing.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 109
Solutions
I'm happy with my solution, this isn't at all straightforward.
/** By Leetcode */
var countNonDecreasingSubarrays = function (nums, k) {
const n = nums.length;
let ans = 0;
let operations = 0;
// Maintain the window maximum with a priority queue of { val, idx }
const q = new MaxPriorityQueue((item) => item.val);
// Record the set of elements covered by this index
const cnt = Array.from({ length: n }, () => []);
// Index of the first greater-or-equal element to the right
const pos = new Array(n).fill(n);
// Decreasing monotonic stack
const st = [];
let left = 0;
for (let right = 0; right < n; right++) {
// Maintain the monotonic stack
while (st.length > 0 && nums[right] >= nums[st[st.length - 1]]) {
pos[st[st.length - 1]] = right;
st.pop();
}
// The stack top is the nearest greater element to the left of nums[right]
if (st.length > 0) {
cnt[st[st.length - 1]].push(right);
}
st.push(right);
// The priority queue front is the window maximum
q.enqueue({ val: nums[right], idx: right });
// Adding nums[right] increases the operation count
operations += Math.max(0, q.front().val - nums[right]);
// The minimum operation count exceeds k; shrink the window
while (operations > k) {
// Removing nums[left] from the window decreases the operation count
for (const i of cnt[left]) {
// Subtract the contribution
operations -=
(nums[left] - nums[i]) * (Math.min(pos[i], right + 1) - i);
}
left++;
}
// Remove elements with indices less than left from the priority queue
while (!q.isEmpty() && q.front().idx < left) {
q.dequeue();
}
// There are right - left + 1 valid contiguous subarrays ending at right
ans += right - left + 1;
}
return ans;
};