|
| 1 | +<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>An integer <code>h</code> is called <strong>valid</strong> if all values in the array that are <strong>strictly greater</strong> than <code>h</code> are <em>identical</em>.</p> |
| 4 | + |
| 5 | +<p>For example, if <code>nums = [10, 8, 10, 8]</code>, a <strong>valid</strong> integer is <code>h = 9</code> because all <code>nums[i] > 9</code> are equal to 10, but 5 is not a <strong>valid</strong> integer.</p> |
| 6 | + |
| 7 | +<p>You are allowed to perform the following operation on <code>nums</code>:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li>Select an integer <code>h</code> that is <em>valid</em> for the <strong>current</strong> values in <code>nums</code>.</li> |
| 11 | + <li>For each index <code>i</code> where <code>nums[i] > h</code>, set <code>nums[i]</code> to <code>h</code>.</li> |
| 12 | +</ul> |
| 13 | + |
| 14 | +<p>Return the <strong>minimum</strong> number of operations required to make every element in <code>nums</code> <strong>equal</strong> to <code>k</code>. If it is impossible to make all elements equal to <code>k</code>, return -1.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<div class="example-block"> |
| 20 | +<p><strong>Input:</strong> <span class="example-io">nums = [5,2,5,4,5], k = 2</span></p> |
| 21 | + |
| 22 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 23 | + |
| 24 | +<p><strong>Explanation:</strong></p> |
| 25 | + |
| 26 | +<p>The operations can be performed in order using valid integers 4 and then 2.</p> |
| 27 | +</div> |
| 28 | + |
| 29 | +<p><strong class="example">Example 2:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><strong>Input:</strong> <span class="example-io">nums = [2,1,2], k = 2</span></p> |
| 33 | + |
| 34 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 35 | + |
| 36 | +<p><strong>Explanation:</strong></p> |
| 37 | + |
| 38 | +<p>It is impossible to make all the values equal to 2.</p> |
| 39 | +</div> |
| 40 | + |
| 41 | +<p><strong class="example">Example 3:</strong></p> |
| 42 | + |
| 43 | +<div class="example-block"> |
| 44 | +<p><strong>Input:</strong> <span class="example-io">nums = [9,7,5,3], k = 1</span></p> |
| 45 | + |
| 46 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 47 | + |
| 48 | +<p><strong>Explanation:</strong></p> |
| 49 | + |
| 50 | +<p>The operations can be performed using valid integers in the order 7, 5, 3, and 1.</p> |
| 51 | +</div> |
| 52 | + |
| 53 | +<p> </p> |
| 54 | +<p><strong>Constraints:</strong></p> |
| 55 | + |
| 56 | +<ul> |
| 57 | + <li><code>1 <= nums.length <= 100 </code></li> |
| 58 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 59 | + <li><code>1 <= k <= 100</code></li> |
| 60 | +</ul> |
0 commit comments