Class Solution
-
- All Implemented Interfaces:
public final class Solution3375 - Minimum Operations to Make Array Values Equal to K.
Easy
You are given an integer array
numsand an integerk.An integer
his called valid if all values in the array that are strictly greater thanhare identical.For example, if
nums = [10, 8, 10, 8], a valid integer ish = 9because allnums[i] > 9are equal to 10, but 5 is not a valid integer.You are allowed to perform the following operation on
nums:Select an integer
hthat is valid for the current values innums.For each index
iwherenums[i] > h, setnums[i]toh.
Return the minimum number of operations required to make every element in
numsequal tok. If it is impossible to make all elements equal tok, return -1.Example 1:
Input: nums = 5,2,5,4,5, k = 2
Output: 2
Explanation:
The operations can be performed in order using valid integers 4 and then 2.
Example 2:
Input: nums = 2,1,2, k = 2
Output: \-1
Explanation:
It is impossible to make all the values equal to 2.
Example 3:
Input: nums = 9,7,5,3, k = 1
Output: 4
Explanation:
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1001 <= k <= 100
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminOperations(IntArray nums, Integer k)-
-
Method Detail
-
minOperations
final Integer minOperations(IntArray nums, Integer k)
-
-
-
-