-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdistant-barcodes.py
50 lines (42 loc) · 1.34 KB
/
distant-barcodes.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
45
46
47
48
49
50
# Time: O(n), k is the number of distinct barcodes
# Space: O(k)
import collections
import itertools
class Solution(object):
def rearrangeBarcodes(self, barcodes):
"""
:type barcodes: List[int]
:rtype: List[int]
"""
k = 2
cnts = collections.Counter(barcodes)
bucket_cnt = max(cnts.itervalues())
result = [0]*len(barcodes)
i = (len(barcodes)-1)%k
for c in itertools.chain((c for c, v in cnts.iteritems() if v == bucket_cnt), (c for c, v in cnts.iteritems() if v != bucket_cnt)):
for _ in xrange(cnts[c]):
result[i] = c
i += k
if i >= len(result):
i = (i-1)%k
return result
# Time: O(n + klogk), k is the number of distinct barcodes
# Space: O(k)
import collections
class Solution2(object):
def rearrangeBarcodes(self, barcodes):
"""
:type barcodes: List[int]
:rtype: List[int]
"""
cnts = collections.Counter(barcodes)
sorted_cnts = [[v, k] for k, v in cnts.iteritems()]
sorted_cnts.sort(reverse=True)
i = 0
for v, k in sorted_cnts:
for _ in xrange(v):
barcodes[i] = k
i += 2
if i >= len(barcodes):
i = 1
return barcodes