forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-excellent-pairs.py
44 lines (37 loc) · 1.15 KB
/
number-of-excellent-pairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Time: O(n + (logn)^2) = O(n)
# Space: O(n + logn) = O(n)
import collections
# bit manipulation, freq table, combinatorics
class Solution(object):
def countExcellentPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def popcount(x):
return bin(x)[2:].count('1')
cnt = collections.Counter(popcount(x) for x in set(nums))
return sum(cnt[i]*cnt[j] for i in cnt.iterkeys() for j in cnt.iterkeys() if i+j >= k)
# Time: O(nlogn)
# Space: O(n)
# bit manipulation, sort, two pointers
class Solution2(object):
def countExcellentPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def popcount(x):
return bin(x)[2:].count('1')
sorted_cnts = sorted(popcount(x) for x in set(nums))
result = 0
left, right = 0, len(sorted_cnts)-1
while left <= right:
if sorted_cnts[left]+sorted_cnts[right] < k:
left += 1
else:
result += 1+2*((right-1)-left+1)
right -= 1
return result