Minimum Operations to Make the Array K-Increasing
master · 1695 · lc hard +32 · verified · 40.2% accepted · 727 likes · top 20%
Description
You are given a 0-indexed array arr of n positive integers and a positive integer k.
The array is called K-increasing if arr[i-k] <= arr[i] for every index i satisfying k <= i <= n-1.
- For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because:
- arr[0] <= arr[2] (4 <= 5)
- arr[1] <= arr[3] (1 <= 2)
- arr[2] <= arr[4] (5 <= 6)
- arr[3] <= arr[5] (2 <= 2)
- However, the same arr is not K-increasing for k = 1 (because arr[0] > arr[1]) or k = 3 (because arr[0] > arr[3]).
In one operation, you can select any index i and assign arr[i] any positive integer value.
Return the minimum number of operations required to make arr K-increasing for the given k.
Example 1:
Example 2:
Example 3:
Code