-
Notifications
You must be signed in to change notification settings - Fork 0
/
2917.py
37 lines (27 loc) · 970 Bytes
/
2917.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
class Solution:
def findKOr(self, nums: List[int], k: int) -> int:
if k == 1:
res = nums[0]
for i in range(1, len(nums)):
res |= nums[i]
return res
elif k == len(nums):
res = nums[0]
for i in range(1, len(nums)):
res &= nums[i]
return res
# index: freq
# freq >= k
freq = collections.defaultdict(int)
for num in nums:
rep = bin(num)[2:]
# print(rep)
for i in range(len(rep)):
if rep[len(rep)-i-1] == '1':
# print(i)
freq[i] += 1
res = 0
for index, freqs in freq.items():
if freqs >= k:
res |= (1 << index)
return res