diff --git a/leetcode/1701-1800/1711.Count-Good-Meals/README.md b/leetcode/1701-1800/1711.Count-Good-Meals/README.md index 751c4edb0..268d5e082 100755 --- a/leetcode/1701-1800/1711.Count-Good-Meals/README.md +++ b/leetcode/1701-1800/1711.Count-Good-Meals/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1701-1800/1711.Count-Good-Meals/Solution.go b/leetcode/1701-1800/1711.Count-Good-Meals/Solution.go index d115ccf5e..f69d971b7 100644 --- a/leetcode/1701-1800/1711.Count-Good-Meals/Solution.go +++ b/leetcode/1701-1800/1711.Count-Good-Meals/Solution.go @@ -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 } diff --git a/leetcode/1701-1800/1711.Count-Good-Meals/Solution_test.go b/leetcode/1701-1800/1711.Count-Good-Meals/Solution_test.go index 14ff50eb4..1b7baf11a 100644 --- a/leetcode/1701-1800/1711.Count-Good-Meals/Solution_test.go +++ b/leetcode/1701-1800/1711.Count-Good-Meals/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }