Menu

3420. Count Non-Decreasing Subarrays After K Operations
πŸ“°

3420. Count Non-Decreasing Subarrays After K Operations

DEV Community: phpΒ·MD ARIFUL HAQUEΒ·5 days ago
#5E1B4NYb
#devto#dev#nums#subarrays#decreasing
Reading 0:00
15s threshold

3420. Count Non-Decreasing Subarrays After K Operations

Difficulty: Hard

Topics: Principal , Array , Stack , Segment Tree , Queue , Sliding Window , Monotonic Stack , Monotonic Queue , Weekly Contest 432

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,…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.