1054. Distant Barcodes

1054. Distant Barcodes

难度: Medium

刷题内容

原题连接

内容描述

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 an answer exists.



Example 1:

Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:

Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]


Note:

1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000

思路 1
- 时间复杂度: O(NlgN)- 空间复杂度: O(N)

统计cnt,然后每次取还剩下个数最多的那一个数字,如果之前刚放进去的就是这个数字那我们就取个数第二多的,一直这样下去即可

```python
from heapq import *

class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
cnt = collections.Counter(barcodes)
heap = []

top Created with Sketch.