Class Solution
java.lang.Object
g2101_2200.s2111_minimum_operations_to_make_the_array_k_increasing.Solution
2111 - Minimum Operations to Make the Array K-Increasing.<p>Hard</p>
<p>You are given a <strong>0-indexed</strong> array <code>arr</code> consisting of <code>n</code> positive integers, and a positive integer <code>k</code>.</p>
<p>The array <code>arr</code> is called <strong>K-increasing</strong> if <code>arr[i-k] <= arr[i]</code> holds for every index <code>i</code>, where <code>k <= i <= n-1</code>.</p>
<ul>
<li>For example, <code>arr = [4, 1, 5, 2, 6, 2]</code> is K-increasing for <code>k = 2</code> because:
<ul>
<li><code>arr[0] <= arr[2] (4 <= 5)</code></li>
<li><code>arr[1] <= arr[3] (1 <= 2)</code></li>
<li><code>arr[2] <= arr[4] (5 <= 6)</code></li>
<li><code>arr[3] <= arr[5] (2 <= 2)</code></li>
</ul>
</li>
<li>However, the same <code>arr</code> is not K-increasing for <code>k = 1</code> (because <code>arr[0] > arr[1]</code>) or <code>k = 3</code> (because <code>arr[0] > arr[3]</code>).</li>
</ul>
<p>In one <strong>operation</strong> , you can choose an index <code>i</code> and <strong>change</strong> <code>arr[i]</code> into <strong>any</strong> positive integer.</p>
<p>Return <em>the <strong>minimum number of operations</strong> required to make the array K-increasing for the given</em> <code>k</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> arr = [5,4,3,2,1], k = 1</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong></p>
<p>For k = 1, the resultant array has to be non-decreasing.</p>
<p>Some of the K-increasing arrays that can be formed are [5, <strong>6</strong> , <strong>7</strong> , <strong>8</strong> , <strong>9</strong> ], [<strong>1</strong> , <strong>1</strong> , <strong>1</strong> , <strong>1</strong> ,1], [<strong>2</strong> , <strong>2</strong> ,3, <strong>4</strong> , <strong>4</strong> ]. All of them require 4 operations.</p>
<p>It is suboptimal to change the array to, for example, [<strong>6</strong> , <strong>7</strong> , <strong>8</strong> , <strong>9</strong> , <strong>10</strong> ] because it would take 5 operations.</p>
<p>It can be shown that we cannot make the array K-increasing in less than 4 operations.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> arr = [4,1,5,2,6,2], k = 2</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>This is the same example as the one in the problem description.</p>
<p>Here, for every index i where 2 <= i <= 5, arr[i-2] <= arr[i].</p>
<p>Since the given array is already K-increasing, we do not need to perform any operations.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> arr = [4,1,5,2,6,2], k = 3</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Indices 3 and 5 are the only ones not satisfying arr[i-3] <= arr[i] for 3 <= i <= 5.</p>
<p>One of the ways we can make the array K-increasing is by changing arr[3] to 4 and arr[5] to 5.</p>
<p>The array will now be [4,1,5, <strong>4</strong> ,6, <strong>5</strong> ].</p>
<p>Note that there can be other ways to make the array K-increasing, but none of them require less than 2 operations.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
<li><code>1 <= arr[i], k <= arr.length</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
kIncreasing
public int kIncreasing(int[] a, int k)
-