Skip to content

Commit 2c44665

Browse files
authored
Merge pull request #1130 from 0xff-dev/1711
Add solution and test-cases for problem 1711
2 parents 505eea8 + c7cfecd commit 2c44665

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

leetcode/1701-1800/1711.Count-Good-Meals/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [1711.Count Good Meals][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+
A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.
5+
6+
You can pick **any** two different foods to make a good meal.
7+
8+
Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the `ith` item of food, return the number of different **good meals** you can make from this list modulo `10^9 + 7`.
9+
10+
Note that items with different indices are considered different even if they have the same deliciousness value.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: deliciousness = [1,3,5,7,9]
16+
Output: 4
17+
Explanation: The good meals are (1,3), (1,7), (3,5) and, (7,9).
18+
Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Good Meals
23-
```go
2423
```
25-
24+
Input: deliciousness = [1,1,1,3,3,3,7]
25+
Output: 15
26+
Explanation: The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.
27+
```
2628

2729
## 结语
2830

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod = 1000000007
4+
5+
func Solution(deliciousness []int) int {
6+
cur := int64(1)
7+
cnt := make(map[int64]int)
8+
for _, n := range deliciousness {
9+
cnt[int64(n)]++
10+
}
11+
ans := 0
12+
for i := 0; i <= 41; i++ {
13+
skip := map[int64]struct{}{}
14+
for d, n := range cnt {
15+
if _, ok := skip[d]; ok {
16+
continue
17+
}
18+
diff := cur - d
19+
if v, ok := cnt[diff]; ok {
20+
skip[diff] = struct{}{}
21+
if diff == d {
22+
ans = (ans + n*(n-1)/2) % mod
23+
} else {
24+
ans = (ans + n*v) % mod
25+
}
26+
}
27+
}
28+
cur <<= 1
29+
}
30+
return ans
531
}

leetcode/1701-1800/1711.Count-Good-Meals/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 3, 5, 7, 9}, 4},
17+
{"TestCase2", []int{1, 1, 1, 3, 3, 3, 7}, 15},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)