Skip to content

Add solution and test-cases for problem 1711 #1130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions leetcode/1701-1800/1711.Count-Good-Meals/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [1711.Count Good Meals][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.

You can pick **any** two different foods to make a good meal.

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`.

Note that items with different indices are considered different even if they have the same deliciousness value.

**Example 1:**

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

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Count Good Meals
```go
```

Input: deliciousness = [1,1,1,3,3,3,7]
Output: 15
Explanation: The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/1701-1800/1711.Count-Good-Meals/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

func Solution(x bool) bool {
return x
const mod = 1000000007

func Solution(deliciousness []int) int {
cur := int64(1)
cnt := make(map[int64]int)
for _, n := range deliciousness {
cnt[int64(n)]++
}
ans := 0
for i := 0; i <= 41; i++ {
skip := map[int64]struct{}{}
for d, n := range cnt {
if _, ok := skip[d]; ok {
continue
}
diff := cur - d
if v, ok := cnt[diff]; ok {
skip[diff] = struct{}{}
if diff == d {
ans = (ans + n*(n-1)/2) % mod
} else {
ans = (ans + n*v) % mod
}
}
}
cur <<= 1
}
return ans
}
13 changes: 6 additions & 7 deletions leetcode/1701-1800/1711.Count-Good-Meals/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 3, 5, 7, 9}, 4},
{"TestCase2", []int{1, 1, 1, 3, 3, 3, 7}, 15},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

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

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