3375. Minimum Operations to Make Array Values Equal to K Easy

@problem@discussion
#Array#Hash Table



1/**
2 * [3375] Minimum Operations to Make Array Values Equal to K
3 *
4 * You are given an integer array nums and an integer k.
5 * An integer h is called valid if all values in the array that are strictly greater than h are identical.
6 * For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 are equal to 10, but 5 is not a valid integer.
7 * You are allowed to perform the following operation on nums:
8 * 
9 * 	Select an integer h that is valid for the current values in nums.
10 * 	For each index i where nums[i] > h, set nums[i] to h.
11 * 
12 * Return the minimum number of operations required to make every element in nums equal to k. If it is impossible to make all elements equal to k, return -1.
13 *  
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [5,2,5,4,5], k = 2</span>
17 * Output: <span class="example-io">2</span>
18 * Explanation:
19 * The operations can be performed in order using valid integers 4 and then 2.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [2,1,2], k = 2</span>
24 * Output: <span class="example-io">-1</span>
25 * Explanation:
26 * It is impossible to make all the values equal to 2.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">nums = [9,7,5,3], k = 1</span>
31 * Output: <span class="example-io">4</span>
32 * Explanation:
33 * The operations can be performed using valid integers in the order 7, 5, 3, and 1.
34 * </div>
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 100 
39 * 	1 <= nums[i] <= 100
40 * 	1 <= k <= 100
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/
46// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_3375() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.