-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...81.LeastNumberofUniqueIntegersafterKRemovals/leastNumberofUniqueIntegersafterKRemovals.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func findLeastNumOfUniqueInts(arr []int, k int) int { | ||
cnt := make(map[int]int) | ||
for _, v := range arr { | ||
cnt[v]++ | ||
} | ||
|
||
n := len(cnt) | ||
nums := make([]int, 0, n) | ||
for _, v := range cnt { | ||
nums = append(nums, v) | ||
} | ||
sort.Ints(nums) | ||
|
||
for i, v := range nums { | ||
k -= v | ||
if k < 0 { | ||
return len(nums) - i | ||
} | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func main() { | ||
arr := []int{4, 3, 1, 1, 3, 3, 2} | ||
k := 3 | ||
|
||
fmt.Println(findLeastNumOfUniqueInts(arr, k)) | ||
} |