Skip to content

Commit 20dcde5

Browse files
committed
Sync LeetCode submission Runtime - 61 ms (86.32%), Memory - 17.8 MB (53.07%)
1 parent 6c630d4 commit 20dcde5

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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] &gt; 9</code>&nbsp;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] &gt; 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>&nbsp;</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>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= nums.length &lt;= 100 </code></li>
58+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
59+
<li><code>1 &lt;= k &lt;= 100</code></li>
60+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Approach 1: Hash Set
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def minOperations(self, nums: List[int], k: int) -> int:
8+
st = set()
9+
for num in nums:
10+
if num < k:
11+
return -1
12+
elif num > k:
13+
st.add(num)
14+
15+
return len(st)
16+

0 commit comments

Comments
 (0)