Skip to content

Commit 412e668

Browse files
add 1711
1 parent 24a036f commit 412e668

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1711|[Count Good Meals](https://leetcode.com/problems/count-good-meals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1711.java) ||Medium|Array, HashTable, Two Pointers|
1112
|1710|[Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1710.java) ||Easy|Greedy, Sort|
1213
|1708|[Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1708.java) ||Easy|Array, Greedy|
1314
|1704|[Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1704.java) ||Easy|String|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1711 {
7+
public static class Solution1 {
8+
private static final long MODUALR = 1000000007;
9+
10+
public int countPairs(int[] deliciousness) {
11+
Map<Integer, Integer> map = new HashMap<>();
12+
long pairs = 0;
13+
for (int i = 0; i < deliciousness.length; i++) {
14+
int power = 1;
15+
for (int j = 0; j < 22; j++) {//we only need to go up to 21 since one of the constraints is: 0 <= deliciousness[i] <= 2 to the power of 20
16+
if (map.containsKey(power - deliciousness[i])) {
17+
pairs += map.get(power - deliciousness[i]);
18+
pairs %= MODUALR;
19+
}
20+
power *= 2;
21+
}
22+
map.put(deliciousness[i], map.getOrDefault(deliciousness[i], 0) + 1);
23+
}
24+
return (int) pairs;
25+
}
26+
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1711;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1711Test {
10+
private static _1711.Solution1 solution1;
11+
private static int[] deliciousness;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1711.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
deliciousness = new int[]{1, 3, 5, 7, 9};
21+
assertEquals(4, solution1.countPairs(deliciousness));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
deliciousness = new int[]{1, 1, 1, 3, 3, 3, 7};
27+
assertEquals(15, solution1.countPairs(deliciousness));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
deliciousness = new int[]{64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64};
33+
assertEquals(528, solution1.countPairs(deliciousness));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)