Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5f7c4d1

Browse files
committed
Add solution #1711
1 parent 51d4f31 commit 5f7c4d1

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,493 LeetCode solutions in JavaScript
1+
# 1,494 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1318,6 +1318,7 @@
13181318
1706|[Where Will the Ball Fall](./solutions/1706-where-will-the-ball-fall.js)|Medium|
13191319
1707|[Maximum XOR With an Element From Array](./solutions/1707-maximum-xor-with-an-element-from-array.js)|Hard|
13201320
1710|[Maximum Units on a Truck](./solutions/1710-maximum-units-on-a-truck.js)|Easy|
1321+
1711|[Count Good Meals](./solutions/1711-count-good-meals.js)|Medium|
13211322
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13221323
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13231324
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|

solutions/1711-count-good-meals.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1711. Count Good Meals
3+
* https://leetcode.com/problems/count-good-meals/
4+
* Difficulty: Medium
5+
*
6+
* A good meal is a meal that contains exactly two different food items with a sum of deliciousness
7+
* equal to a power of two.
8+
*
9+
* You can pick any two different foods to make a good meal.
10+
*
11+
* Given an array of integers deliciousness where deliciousness[i] is the deliciousness of the
12+
* ith item of food, return the number of different good meals you can make from this list
13+
* modulo 109 + 7.
14+
*
15+
* Note that items with different indices are considered different even if they have the same
16+
* deliciousness value.
17+
*/
18+
19+
/**
20+
* @param {number[]} deliciousness
21+
* @return {number}
22+
*/
23+
var countPairs = function(deliciousness) {
24+
const MOD = 1e9 + 7;
25+
const frequency = new Map();
26+
let result = 0;
27+
28+
for (const value of deliciousness) {
29+
for (let power = 1; power <= 1 << 21; power <<= 1) {
30+
const complement = power - value;
31+
if (frequency.has(complement)) {
32+
result = (result + frequency.get(complement)) % MOD;
33+
}
34+
}
35+
frequency.set(value, (frequency.get(value) || 0) + 1);
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)