Skip to content

Commit 39dc9d1

Browse files
committed
solutions: 3375. Minimum Operations to Make Array Values Equal to K (Easy)
1 parent 2b9eb5f commit 39dc9d1

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
description: 'Author: @wkw | https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/'
3+
---
4+
5+
# 3375. Minimum Operations to Make Array Values Equal to K (Easy)
6+
7+
## Problem Link
8+
9+
https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k
10+
11+
## Problem Statement
12+
13+
You are given an integer array nums and an integer k.
14+
15+
An integer h is called valid if all values in the array that are strictly greater than h are identical.
16+
17+
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.
18+
19+
You are allowed to perform the following operation on nums:
20+
21+
Select an integer h that is valid for the current values in nums. For each index i where nums[i] > h, set nums[i] to h. 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.
22+
23+
**Example 1:**
24+
25+
**Input:** nums = [5,2,5,4,5], k = 2
26+
27+
**Output:** 2
28+
29+
**Explanation:**
30+
31+
The operations can be performed in order using valid integers 4 and then 2.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [2,1,2], k = 2
36+
37+
**Output:** -1
38+
39+
**Explanation:**
40+
41+
It is impossible to make all the values equal to 2.
42+
43+
**Example 3:**
44+
45+
**Input:** nums = [9,7,5,3], k = 1
46+
47+
**Output:** 4
48+
49+
**Explanation:**
50+
51+
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
52+
53+
**Constraints:**
54+
55+
- $1 <= nums.length <= 100$
56+
- $1 <= nums[i] <= 100$
57+
- $1 <= k <= 100$
58+
59+
## Approach 1: Check Distinct Numbers
60+
61+
<Tabs>
62+
<TabItem value="py" label="Python">
63+
<SolutionAuthor name="@wkw"/>
64+
65+
```py
66+
# TC: O(n)
67+
# SC: O(1)
68+
class Solution:
69+
def minOperations(self, nums: List[int], k: int) -> int:
70+
# we can just focus on distinct numbers in $nums$
71+
72+
# in example 1, d = {5, 4, 2}
73+
# we can see that the numbers > 2 are 5 and 4, so the answer is 2
74+
# in op1, we can choose h = 4 to make [5,2,5,4,5] to [4,2,4,4,4]
75+
# in op2, we can choose h = 2 to make [4,2,4,4,4] to [2,2,2,2,2]
76+
77+
# in example 2, d = {1, 2}
78+
# since 1 < 2 so it is not possible to make all elements equal to k,
79+
# hence we return -1 in this case
80+
81+
# if there is a number $x$ that is less than $k$,
82+
# then return -1
83+
if any(x < k for x in nums): return -1
84+
# otherwise check the size of the unique number that is greater than k
85+
return len(set(x for x in nums if x > k))
86+
```
87+
88+
</TabItem>
89+
</Tabs>

0 commit comments

Comments
 (0)