Skip to content

Commit

Permalink
Merge pull request #786 from 0xff-dev/1481
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1481
  • Loading branch information
6boris committed Mar 23, 2024
2 parents c2531aa + cdd8435 commit 4d40c88
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 11 deletions.
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
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
arr []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{5, 5, 1}, 1, 1},
{"TestCase2", []int{4, 3, 1, 1, 3, 3, 2}, 3, 2},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.arr, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.arr, c.k)
}
got = Solution2(c.arr, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.arr, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 4d40c88

Please sign in to comment.