#3375

Minimum Operations to Make Array Values Equal to K

pupil · 315 · lc easy +21 · 73.3% accepted · 406 likes · top 83%

Description

You are given an integer array nums and an integer k.

An integer h is called valid if all values in nums strictly greater than h are identical.

You may perform the following operation:

- Choose a valid integer h.

- Set every element nums[i] > h to h.

Return the minimum number of operations needed to make every element equal to k. If it is impossible, return -1.

Code

1
2
3