Class Solution
-
- All Implemented Interfaces:
public final class Solution3420 - Count Non-Decreasing Subarrays After K Operations.
Hard
You are given an array
numsofnintegers and an integerk.For each subarray of
nums, you can apply up tokoperations 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
koperations.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 is21 - 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 ofnumswith three or fewer elements, except[6, 3, 1], can be made non-decreasing afterkoperations. 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 are1 + 5 + 4 + 2 = 12subarrays that can be made non-decreasing.Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
<code>1 <= k <= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final LongcountNonDecreasingSubarrays(IntArray nums, Integer k)-
-
Method Detail
-
countNonDecreasingSubarrays
final Long countNonDecreasingSubarrays(IntArray nums, Integer k)
-
-
-
-