Skip to content

Commit 29cc4c0

Browse files
authored
Create 1481_Least_Number_of_Unique_Integers_after_K_Removals.md
1 parent c25ba0d commit 29cc4c0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## 1481. Least Number of Unique Integers after K Removals
2+
3+
4+
Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.
5+
6+
7+
8+
Example 1:
9+
10+
Input: arr = [5,5,4], k = 1
11+
Output: 1
12+
Explanation: Remove the single 4, only 5 is left.
13+
Example 2:
14+
Input: arr = [4,3,1,1,3,3,2], k = 3
15+
Output: 2
16+
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
17+
18+
19+
Constraints:
20+
21+
1 <= arr.length <= 10^5
22+
1 <= arr[i] <= 10^9
23+
0 <= k <= arr.length
24+
25+
```python
26+
def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:
27+
dic = Counter(arr)
28+
sorted_dic = sorted(dic.items(), key=lambda x:x[1])
29+
c = 0
30+
for v in sorted_dic:
31+
k=k-v[1]
32+
c+=1
33+
if k ==0:
34+
return len(dic)-c
35+
if k <0:
36+
return len(dic)-c+1
37+
```
38+
39+
40+
```
41+
Runtime: 444 ms, faster than 80.76% of Python3 online submissions for Least Number of Unique Integers after K Removals.
42+
Memory Usage: 33.6 MB, less than 5.73% of Python3 online submissions for Least Number of Unique Integers after K Removals.
43+
```

0 commit comments

Comments
 (0)