-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path477.py
35 lines (35 loc) · 1.24 KB
/
477.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
__________________________________________________________________________________________________
sample 316 ms submission
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
xmap = map("{:032b}".format, nums)
total, N = 0, len(nums)
for i in zip(*xmap):
ones = i.count('1')
total += ones * (N - ones)
return total
__________________________________________________________________________________________________
sample 14108 kb submission
import heapq
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
if len(nums)<=1: return 0
max_val=max(nums)
index=curr=1
while curr<=max_val:
curr=2**index
index+=1
index-=2
heapq.heapify(nums)
ans=0
#print(index)
for i in range(index,-1,-1):
left=[]
while nums and nums[0]<2**i:
left.append(heapq.heappop(nums))
#print(left,nums)
ans+=len(left)*len(nums)
nums=[a-2**i for a in nums]+left
heapq.heapify(nums)
return ans
__________________________________________________________________________________________________