-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #786 from 0xff-dev/1481
Add solution and test-cases for problem 1481
- Loading branch information
Showing
3 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
leetcode/1401-1500/1481.Least-Number-of-Unique-Integers-after-K-Removals/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# [1481.Least Number of Unique Integers after K Removals][title] | ||
|
||
## Description | ||
Given an array of integers `arr` and an integer `k`. Find the least number of unique integers after removing **exactly** `k` elements. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: arr = [5,5,4], k = 1 | ||
Output: 1 | ||
Explanation: Remove the single 4, only 5 is left. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: arr = [4,3,1,1,3,3,2], k = 3 | ||
Output: 2 | ||
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
84 changes: 83 additions & 1 deletion
84
leetcode/1401-1500/1481.Least-Number-of-Unique-Integers-after-K-Removals/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,87 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
import ( | ||
"container/heap" | ||
) | ||
|
||
type eleCount struct { | ||
n, c int | ||
} | ||
|
||
type eleCountList []eleCount | ||
|
||
func (e *eleCountList) Len() int { | ||
return len(*e) | ||
} | ||
|
||
func (e *eleCountList) Less(i, j int) bool { | ||
return (*e)[i].c < (*e)[j].c | ||
} | ||
func (e *eleCountList) Swap(i, j int) { | ||
(*e)[i], (*e)[j] = (*e)[j], (*e)[i] | ||
} | ||
|
||
func (e *eleCountList) Push(x interface{}) { | ||
*e = append(*e, x.(eleCount)) | ||
} | ||
|
||
func (e *eleCountList) Pop() interface{} { | ||
old := *e | ||
l := len(old) | ||
x := old[l-1] | ||
*e = old[:l-1] | ||
return x | ||
} | ||
func Solution(arr []int, k int) int { | ||
nodeCache := make(map[int]int) | ||
list := eleCountList{} | ||
for _, n := range arr { | ||
idx, ok := nodeCache[n] | ||
if !ok { | ||
list = append(list, eleCount{n: n, c: 1}) | ||
p := len(list) - 1 | ||
nodeCache[n] = p | ||
continue | ||
} | ||
list[idx].c++ | ||
} | ||
heap.Init(&list) | ||
add := 0 | ||
for k > 0 && list.Len() > 0 { | ||
top := heap.Pop(&list).(eleCount) | ||
if top.c >= k { | ||
if top.c != k { | ||
add = 1 | ||
} | ||
break | ||
} | ||
k -= top.c | ||
} | ||
return list.Len() + add | ||
} | ||
|
||
func Solution2(arr []int, k int) int { | ||
count := make(map[int]int) | ||
for _, n := range arr { | ||
count[n]++ | ||
} | ||
cc := [100001]int{} | ||
for _, c := range count { | ||
cc[c]++ | ||
} | ||
remove := 0 | ||
for i := 1; i <= 100000; i++ { | ||
if cc[i] == 0 { | ||
continue | ||
} | ||
|
||
a := k / i | ||
if a < cc[i] { | ||
remove += a | ||
break | ||
} | ||
k -= cc[i] * i | ||
remove += cc[i] | ||
} | ||
return len(count) - remove | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters