Welcome to Subscribe On Youtube
3420. Count Non-Decreasing Subarrays After K Operations
Description
You 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
Solution 1
-
class Solution { public long countNonDecreasingSubarrays(int[] nums, int k) { int n = nums.length; Deque<Integer> q = new ArrayDeque<>(); long ans = 0; long cost = 0; int r = n - 1; for (int l = n - 1; l >= 0; --l) { int x = nums[l]; while (!q.isEmpty() && nums[q.peekLast()] <= x) { int s = q.pollLast(); int end = q.isEmpty() ? r + 1 : q.peekLast(); cost += (long) (end - s) * (x - nums[s]); } q.offerLast(l); while (cost > k) { cost -= nums[q.peekFirst()] - nums[r]; if (q.peekFirst() == r) { q.pollFirst(); } --r; } ans += r - l + 1; } return ans; } } -
class Solution { public: long long countNonDecreasingSubarrays(vector<int>& nums, int k) { int n = nums.size(); deque<int> q; long long ans = 0, cost = 0; int r = n - 1; for (int l = n - 1; l >= 0; --l) { int x = nums[l]; while (!q.empty() && nums[q.back()] <= x) { int s = q.back(); q.pop_back(); int end = q.empty() ? r + 1 : q.back(); cost += 1LL * (end - s) * (x - nums[s]); } q.push_back(l); while (cost > k) { cost -= nums[q.front()] - nums[r]; if (q.front() == r) { q.pop_front(); } --r; } ans += r - l + 1; } return ans; } }; -
class Solution: def countNonDecreasingSubarrays(self, nums: list[int], k: int) -> int: n = len(nums) q = deque() ans = 0 cost = 0 r = n - 1 for l in range(n - 1, -1, -1): x = nums[l] while q and nums[q[-1]] <= x: s = q.pop() end = q[-1] if q else r + 1 cost += (end - s) * (x - nums[s]) q.append(l) while cost > k: cost -= nums[q[0]] - nums[r] if q[0] == r: q.popleft() r -= 1 ans += r - l + 1 return ans -
func countNonDecreasingSubarrays(nums []int, k int) int64 { n := len(nums) q := []int{} var ans, cost int64 r := n - 1 for l := n - 1; l >= 0; l-- { x := nums[l] for len(q) > 0 && nums[q[len(q)-1]] <= x { s := q[len(q)-1] q = q[:len(q)-1] end := r + 1 if len(q) > 0 { end = q[len(q)-1] } cost += int64(end-s) * int64(x-nums[s]) } q = append(q, l) for cost > int64(k) { cost -= int64(nums[q[0]] - nums[r]) if q[0] == r { q = q[1:] } r-- } ans += int64(r - l + 1) } return ans } -
function countNonDecreasingSubarrays(nums: number[], k: number): number { const n = nums.length; const q: number[] = []; let head = 0; let ans = 0; let cost = 0; let r = n - 1; for (let l = n - 1; l >= 0; l--) { const x = nums[l]; while (head < q.length && nums[q[q.length - 1]] <= x) { const s = q.pop()!; const end = head < q.length ? q[q.length - 1] : r + 1; cost += (x - nums[s]) * (end - s); } q.push(l); while (cost > k) { cost -= nums[q[head]] - nums[r]; if (q[head] === r) { head++; } r--; } ans += r - l + 1; } return ans; } -
use std::collections::VecDeque; impl Solution { pub fn count_non_decreasing_subarrays(nums: Vec<i32>, k: i32) -> i64 { let n = nums.len(); let mut q: VecDeque<usize> = VecDeque::new(); let mut ans: i64 = 0; let mut cost: i64 = 0; let mut r = n as i64 - 1; let k = k as i64; for l in (0..n).rev() { let x = nums[l] as i64; while let Some(&s) = q.back() { if nums[s] as i64 > x { break; } q.pop_back(); let end = q.back().copied().map(|idx| idx as i64).unwrap_or(r + 1); cost += (x - nums[s] as i64) * (end - s as i64); } q.push_back(l); while cost > k { let s = *q.front().unwrap(); cost -= nums[s] as i64 - nums[r as usize] as i64; if s as i64 == r { q.pop_front(); } r -= 1; } ans += r - l as i64 + 1; } ans } }