Skip to content

Commit 9a63033

Browse files
authored
Merge pull request #781 from 0xff-dev/1090
Add solution and test-cases for problem 1090
2 parents 1614e3f + c60f304 commit 9a63033

File tree

3 files changed

+83
-25
lines changed

3 files changed

+83
-25
lines changed

leetcode/1001-1100/1090.Largest-Values-From-Labels/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [1090.Largest Values From Labels][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a set of `n` items. You are given two integer arrays `values` and `values` where the value and the label of the i<sup>th</sup> element are `values[i]` and `labels[i]` respectively. You are also given two integers `numWanted` and `useLimit`.
5+
6+
Choose a subset `s` of the `n` elements such that:
7+
8+
- The size of the subset `s` is **less than or equal to** `numWanted`.
9+
- There are **at most** `useLimit` items with the same label in `s`.
10+
11+
The **score** of a subset is the sum of the values in the subset.
12+
13+
Return the maximum **score** of a subset `s`.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
19+
Output: 9
20+
Explanation: The subset chosen is the first, third, and fifth items.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Largest Values From Labels
23-
```go
2425
```
26+
Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
27+
Output: 12
28+
Explanation: The subset chosen is the first, second, and third items.
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1
35+
Output: 16
36+
Explanation: The subset chosen is the first and fourth items.
37+
```
2638

2739
## 结语
2840

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
)
6+
7+
type heap1090 struct {
8+
values []int
9+
data []int
10+
}
11+
12+
func (h *heap1090) Len() int {
13+
return len(h.data)
14+
}
15+
16+
func (h *heap1090) Less(i, j int) bool {
17+
return h.values[h.data[i]] > h.values[h.data[j]]
18+
}
19+
func (h *heap1090) Swap(i, j int) {
20+
h.data[i], h.data[j] = h.data[j], h.data[i]
21+
}
22+
func (h *heap1090) Push(x interface{}) {
23+
h.data = append(h.data, x.(int))
24+
}
25+
func (h *heap1090) Pop() interface{} {
26+
old := h.data
27+
l := len(old)
28+
x := old[l-1]
29+
h.data = old[:l-1]
430
return x
531
}
32+
33+
func Solution(values []int, labels []int, numWanted int, useLimit int) int {
34+
group := make(map[int]int)
35+
h := heap1090{data: make([]int, 0), values: values}
36+
for i := 0; i < len(values); i++ {
37+
heap.Push(&h, i)
38+
}
39+
ans := 0
40+
for h.Len() > 0 && numWanted > 0 {
41+
top := heap.Pop(&h).(int)
42+
if group[labels[top]] >= useLimit {
43+
continue
44+
}
45+
ans += values[top]
46+
group[labels[top]]++
47+
numWanted--
48+
}
49+
return ans
50+
}

leetcode/1001-1100/1090.Largest-Values-From-Labels/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
values, labels []int
14+
numWanted, useLimit int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{5, 4, 3, 2, 1}, []int{1, 1, 2, 2, 3}, 3, 1, 9},
18+
{"TestCase2", []int{5, 4, 3, 2, 1}, []int{1, 3, 3, 3, 2}, 3, 2, 12},
19+
{"TestCase3", []int{9, 8, 8, 7, 6}, []int{0, 0, 0, 1, 1}, 3, 1, 16},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.values, c.labels, c.numWanted, c.useLimit)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
28+
c.expect, got, c.values, c.labels, c.numWanted, c.useLimit)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)