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
|
# 1054. Distant Barcodes
from collections import Counter
"""
in a warehouse, there is a row of barcodes where the i'th barcode is
'barcodes[i]'. rearrange the barcodes so that no two adjacent barcodes are
equal. you may return any answer, and it is guaranteed that an answer exists
"""
class Solution(object):
def rearrangeBarcodes(self, barcodes):
"""
:type barcodes: List[int]
:rtype: List[int]
"""
cnt = Counter(barcodes)
barcodes.sort(key=lambda b: (cnt[b], b))
barcodes[1::2], barcodes[::2] = (
barcodes[0 : len(barcodes) / 2],
barcodes[len(barcodes) / 2 :],
)
return barcodes
if __name__ == "__main__":
obj = Solution()
print(obj.rearrangeBarcodes(barcodes=[1, 1, 1, 2, 2, 2]))
print(obj.rearrangeBarcodes(barcodes=[1, 1, 1, 1, 2, 2, 3, 3]))
|