You're given an array nums of n integers and an integer k representing the maximum number of operations allowed.
Your goal: Count how many subarrays can be made non-decreasing using at most k operations.
Operations: For each subarray, you can increment any element by 1 up to k times total. Each subarray is considered independently - changes don't carry over.
Non-decreasing: An array where each element is greater than or equal to the previous element (e.g., [1, 2, 2, 5]).
Example: Given nums = [2, 1, 3] and k = 1, the subarray [2, 1] can become [2, 2] with 1 operation, making it non-decreasing.
Input & Output
Visualization
Time & Space Complexity
O(n²) subarrays à O(n) to check each subarray
Only using constant extra space for calculations
Constraints
- 1 ⤠nums.length ⤠1000
- 1 ⤠nums[i] ⤠109
- 0 ⤠k ⤠109
- Each subarray is considered independently