Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit412e668

Browse files
add 1711
1 parent24a036f commit412e668

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclass_1711 {
7+
publicstaticclassSolution1 {
8+
privatestaticfinallongMODUALR =1000000007;
9+
10+
publicintcountPairs(int[]deliciousness) {
11+
Map<Integer,Integer>map =newHashMap<>();
12+
longpairs =0;
13+
for (inti =0;i <deliciousness.length;i++) {
14+
intpower =1;
15+
for (intj =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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1711;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1711Test {
10+
privatestatic_1711.Solution1solution1;
11+
privatestaticint[]deliciousness;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1711.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
deliciousness =newint[]{1,3,5,7,9};
21+
assertEquals(4,solution1.countPairs(deliciousness));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
deliciousness =newint[]{1,1,1,3,3,3,7};
27+
assertEquals(15,solution1.countPairs(deliciousness));
28+
}
29+
30+
@Test
31+
publicvoidtest3() {
32+
deliciousness =newint[]{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)

[8]ページ先頭

©2009-2025 Movatter.jp