Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 503 Bytes

1054.md

File metadata and controls

19 lines (17 loc) · 503 Bytes

1054. Distant Barcodes

Solution 1 (time O(nlog(n)), space O(n))

class Solution(object):
    def rearrangeBarcodes(self, barcodes):
        """
        :type barcodes: List[int]
        :rtype: List[int]
        """
        cnt = collections.Counter(barcodes)
        barcodes.sort(key=lambda x: (-cnt[x], x))
        n = len(barcodes)
        ans = [0] * n
        ans[::2] = barcodes[:(n + 1) // 2]
        ans[1::2] = barcodes[(n + 1) // 2:]
        return ans