Skip to content

Commit 0157340

Browse files
committed
New Problem Solution -"1711. Count Good Meals"
1 parent 06eab70 commit 0157340

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ LeetCode
6363
|1725|[Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/) | [C++](./algorithms/cpp/numberOfRectanglesThatCanFormTheLargestSquare/NumberOfRectanglesThatCanFormTheLargestSquare.cpp)|Easy|
6464
|1717|[Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/) | [C++](./algorithms/cpp/maximumScoreFromRemovingSubstrings/MaximumScoreFromRemovingSubstrings.cpp)|Medium|
6565
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/) | [C++](./algorithms/cpp/calculateMoneyInLeetcodeBank/CalculateMoneyInLeetcodeBank.cpp)|Easy|
66+
|1711|[Count Good Meals](https://leetcode.com/problems/count-good-meals/) | [C++](./algorithms/cpp/countGoodMeals/CountGoodMeals.cpp)|Medium|
6667
|1710|[Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | [C++](./algorithms/cpp/maximumUnitsOnATruck/MaximumUnitsOnATruck.cpp)|Easy|
6768
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/) | [C++](./algorithms/cpp/lexicographicallySmallestStringAfterApplyingOperations/LexicographicallySmallestStringAfterApplyingOperations.cpp)|Medium|
6869
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/) | [C++](./algorithms/cpp/largestSubstringBetweenTwoEqualCharacters/LargestSubstringBetweenTwoEqualCharacters.cpp)|Easy|
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Source : https://leetcode.com/problems/count-good-meals/
2+
// Author : Hao Chen
3+
// Date : 2021-03-30
4+
5+
/*****************************************************************************************************
6+
*
7+
* A good meal is a meal that contains exactly two different food items with a sum of deliciousness
8+
* equal to a power of two.
9+
*
10+
* You can pick any two different foods to make a good meal.
11+
*
12+
* Given an array of integers deliciousness where deliciousness[i] is the deliciousness of the i^​​​​​​
13+
* th​​​​​​​​ item of food, return the number of different good meals you can make from this list
14+
* modulo 10^9 + 7.
15+
*
16+
* Note that items with different indices are considered different even if they have the same
17+
* deliciousness value.
18+
*
19+
* Example 1:
20+
*
21+
* Input: deliciousness = [1,3,5,7,9]
22+
* Output: 4
23+
* Explanation: The good meals are (1,3), (1,7), (3,5) and, (7,9).
24+
* Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
25+
*
26+
* Example 2:
27+
*
28+
* Input: deliciousness = [1,1,1,3,3,3,7]
29+
* Output: 15
30+
* Explanation: The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.
31+
*
32+
* Constraints:
33+
*
34+
* 1 <= deliciousness.length <= 10^5
35+
* 0 <= deliciousness[i] <= 2^20
36+
******************************************************************************************************/
37+
38+
class Solution {
39+
public:
40+
int countPairs(vector<int>& deliciousness) {
41+
const int MAX_EXP = 22;
42+
int pow2[MAX_EXP];
43+
for (int i=0; i<MAX_EXP; i++){
44+
pow2[i] = 1 << i;
45+
//cout << pow2[i] << ", ";
46+
}
47+
48+
unordered_map<int, int> stat;
49+
int big = 0;
50+
for(auto& d: deliciousness){
51+
stat[d]++;
52+
53+
}
54+
55+
long m = 0;
56+
for(auto& d: deliciousness){
57+
for(int i=MAX_EXP-1; i>=0 && pow2[i] >= d; i--){
58+
int x = pow2[i] - d;
59+
if ( stat.find(x) != stat.end() ){
60+
m += (x==d) ? stat[x]-1 : stat[x];
61+
}
62+
}
63+
}
64+
65+
// remove the duplication - m/2,
66+
// because both [1,3] and [3,1] are counted.
67+
return (m/2) % 1000000007;
68+
}
69+
};

0 commit comments

Comments
 (0)