File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ # https://leetcode.com/problems/distant-barcodes/
2
+ # Medium (34.24%)
3
+ # Total Accepted: 2,331
4
+ # Total Submissions: 6,808
5
+
6
+
7
+ from heapq import heappush , heappop
8
+
9
+
10
+ class Solution (object ):
11
+ def rearrangeBarcodes (self , barcodes ):
12
+ """
13
+ :type barcodes: List[int]
14
+ :rtype: List[int]
15
+ """
16
+ length = len (barcodes )
17
+ if length < 3 :
18
+ return barcodes
19
+
20
+ hash_map = {}
21
+
22
+ for n in barcodes :
23
+ hash_map [n ] = hash_map .get (n , 0 ) + 1
24
+
25
+ h = []
26
+
27
+ for k , v in hash_map .iteritems ():
28
+ heappush (h , (- v , k ))
29
+
30
+ i = 0
31
+ while h :
32
+ tmp = []
33
+ j = 2
34
+ while j > 0 and h :
35
+ item = heappop (h )
36
+ barcodes [i ] = item [1 ]
37
+ i += 1
38
+ if item [0 ] + 1 != 0 :
39
+ tmp += (item [0 ] + 1 , item [1 ]),
40
+ j -= 1
41
+ if tmp :
42
+ for item in tmp :
43
+ heappush (h , item )
44
+
45
+
46
+ return barcodes
You can’t perform that action at this time.
0 commit comments