Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Feb 16, 2024
1 parent 9bb36d2 commit e379dae
Showing 1 changed file with 38 additions and 0 deletions.
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))
}

0 comments on commit e379dae

Please sign in to comment.