Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions leetcode3/이진희/1711. Count Good Meals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

/*
1. 아이디어: 해쉬맵 사용, 2의 거듭제곱은 최대 22번이므로 시간 안에 통과 가능

2. 시간복잡도: O(n*21)

3. 자료구조/알고리즘: 해쉬맵

*/
class Solution {
public int countPairs(int[] deliciousness) {
// 합의 차이가 정확히 2의 거듭제곱인 서로 다른 원소
// 인덱스로 파악
// 10^9 + 7 모듈러 연산

int MOD = 1000000007;
long ans = 0L;

Map<Integer, Integer> map = new HashMap<>();
for(int i : deliciousness) {
int food = i;
int num = 1;

for(int j=0; j<=21; j++) {
int tmp = num - food;

if(map.containsKey(tmp)) ans = (ans+map.get(tmp))%MOD;
num*=2;
}
map.put(food, map.getOrDefault(food, 0)+1);
}

return (int)ans;
}
}