Intuition
Reverse the array so fixing a subarray becomes a left-to-right sweep where each larger value lifts smaller ones before it; keep the running lift cost and a two-pointer window to count, for every right end, the valid start positions.
Explanation
The cost of fixing one subarray
We may only increment, never decrease. So to make a subarray non-decreasing with the fewest operations, scan it left to right and raise every element up to the biggest value seen so far (the running prefix maximum). The cost is the sum of those lifts.
For [6, 3, 1] the prefix maxima are 6, 6, 6, so the lift is (6-6) + (6-3) + (6-1) = 8. With a budget of k = 7 this subarray cannot be fixed.
Counting all subarrays efficiently
Trying every subarray and summing its lift is O(n^3). Two observations collapse it to O(n):
- For a fixed left end, extending the right end never lowers the cost. So for each right end there is a single furthest-left start that still fits the budget. That is a sliding window with a left pointer that only moves right.
- The hard part is updating the cost when the window changes. We make it cheap by reversing the array first. In the reversed array, sweeping left to right means each new, larger value lifts the smaller values that came before it. We keep those values on a monotonic stack as
(value, count)runs, so one larger value can lift a whole run in O(1) amortized, and the total lift (cost) is maintained incrementally.
The bottom of the stack always holds the window's maximum, which is exactly the lifted value of the window's oldest element. So when the window shrinks from the left we subtract that element's lift and shrink the bottom run.
Walk-through on nums = [6, 3, 1, 3, 6], k = 4
Reverse to a = [6, 3, 1, 3, 6]. Sweep right left to right, keeping cost, a left pointer, and the stack.
right = 0value6: stack[6x1], cost0. Window[0,0]adds 1 subarray. Total 1.right = 1value3:3 < 6, push[6x1, 3x1], cost0. Window[0,1]adds 2. Total 3.right = 2value1: push[6x1, 3x1, 1x1], cost0. Window[0,2]adds 3. Total 6.right = 3value3:3 > 1, pop1x1and lift it to 3 (cost+2). Push[6x1, 3x2], cost2. Still<= 4. Window[0,3]adds 4. Total 10.right = 4value6:6 > 3, pop the3x2run and lift both to 6 (cost+6), giving stack[6x1, 6x3]and cost2 + 6 = 8 > 4. Now shrink from the left: drop the oldest element (value6), its lift is6-6 = 0, so cost stays8; drop the next oldest (value3), removing lift6-3 = 3, cost5; drop the next (value1), removing lift6-1 = 5, cost0 <= 4. Left is now at index 3. Window[3,4]adds 2. Total 12.
Answer: 12.
Complexity
- Time: O(n). Each element is pushed and popped from the stack at most once, and each pointer only moves forward.
- Space: O(n) for the stack in the worst case (a strictly decreasing array).
Implementation
class Solution {
public long countNonDecreasingSubarrays(int[] nums, int k) {
int n = nums.length;
// Reverse: "make non-decreasing" becomes a left-to-right lift sweep where
// each new, larger value lifts the smaller values that came before it.
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nums[n - 1 - i];
// Monotonic stack of (value, count) runs in vals[]/cnts[], non-increasing
// from index `head` (the window maximum) up to `top - 1`.
long[] vals = new long[n + 1];
long[] cnts = new long[n + 1];
int top = 0, head = 0, left = 0;
long ans = 0, cost = 0;
for (int right = 0; right < n; right++) {
long v = a[right];
long cnt = 1;
while (top > head && vals[top - 1] < v) {
top--;
cost += (v - vals[top]) * cnts[top];
cnt += cnts[top];
}
vals[top] = v;
cnts[top] = cnt;
top++;
// Shrink from the left while over budget; the oldest element's lifted
// value is the window maximum = the bottom run's value.
while (cost > k) {
cost -= vals[head] - a[left];
cnts[head]--;
left++;
if (cnts[head] == 0) head++;
}
ans += right - left + 1;
}
return ans;
}
}