38
38
39
39
<!-- 这里可写通用的实现逻辑 -->
40
40
41
- ** 方法一:贪心 + 哈希表 + 优先队列(大根堆) **
41
+ ** 方法一:计数 + 排序 **
42
42
43
- 先用哈希表 ` cnt ` 统计每种条形码的数量,然后将每种条形码及其数量存入优先队列(大根堆) 中,优先队列中的元素按照条形码数量从大到小排序 。
43
+ 我们先用哈希表或数组 $ cnt$ 统计数组 $barcodes$ 中各个数出现的次数,然后将 $barcodes$ 中的数按照它们在 $cnt$ 中出现的次数从大到小排序,如果出现次数相同,那么就按照数的大小从小到大排序(确保相同的数相邻) 。
44
44
45
- 重排条形码时,我们每次从堆顶弹出一个元素 ` (v, k) ` ,将 ` k ` 添加到结果数组中,并将 ` (v-1, k) ` 放入队列 ` q ` 中。当队列长度大于 $1$ 时,弹出队首元素 ` p ` ,若此时 ` p.v ` 大于 $0$,则将 ` p ` 放入堆中。循环,直至堆为空 。
45
+ 接下来,我们创建一个长度为 $n$ 的答案数组 $ans$,然后遍历排好序的 $barcodes$,将元素依次填入答案数组的 $0, 2, 4, \cdots$ 等偶数下标位置,然后将剩余元素依次填入答案数组的 $1, 3, 5, \cdots$ 等奇数下标位置即可 。
46
46
47
- 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为条形码数组的长度。
48
-
49
- 相似题目:[ 767. 重构字符串] ( /solution/0700-0799/0767.Reorganize%20String/README.md )
47
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别是数组 $barcodes$ 的长度以及数组 $barcodes$ 中的最大值。
50
48
51
49
<!-- tabs:start -->
52
50
58
56
class Solution :
59
57
def rearrangeBarcodes (self , barcodes : List[int ]) -> List[int ]:
60
58
cnt = Counter(barcodes)
61
- h = [(- v, k) for k, v in cnt.items()]
62
- heapify(h)
63
- q = deque()
64
- ans = []
65
- while h:
66
- v, k = heappop(h)
67
- ans.append(k)
68
- q.append((v + 1 , k))
69
- while len (q) > 1 :
70
- p = q.popleft()
71
- if p[0 ]:
72
- heappush(h, p)
59
+ barcodes.sort(key = lambda x : (- cnt[x], x))
60
+ n = len (barcodes)
61
+ ans = [0 ] * len (barcodes)
62
+ ans[::2 ] = barcodes[: (n + 1 ) // 2 ]
63
+ ans[1 ::2 ] = barcodes[(n + 1 ) // 2 :]
73
64
return ans
74
65
```
75
66
@@ -80,27 +71,22 @@ class Solution:
80
71
``` java
81
72
class Solution {
82
73
public int [] rearrangeBarcodes (int [] barcodes ) {
83
- Map<Integer , Integer > cnt = new HashMap<> ();
84
- for (int v : barcodes) {
85
- cnt. put(v, cnt. getOrDefault(v, 0 ) + 1 );
74
+ int n = barcodes. length;
75
+ Integer [] t = new Integer [n];
76
+ int mx = 0 ;
77
+ for (int i = 0 ; i < n; ++ i) {
78
+ t[i] = barcodes[i];
79
+ mx = Math . max(mx, barcodes[i]);
86
80
}
87
- PriorityQueue< int[]> pq = new PriorityQueue<> ((a, b) - > b[ 1 ] - a[ 1 ]) ;
88
- for (var e : cnt . entrySet() ) {
89
- pq . offer( new int [] {e . getKey(), e . getValue()}) ;
81
+ int [] cnt = new int [mx + 1 ] ;
82
+ for (int x : barcodes ) {
83
+ ++ cnt[x] ;
90
84
}
91
- Deque<int[]> q = new ArrayDeque<> ();
92
- int [] ans = new int [barcodes. length];
93
- int i = 0 ;
94
- while (! pq. isEmpty()) {
95
- var p = pq. poll();
96
- ans[i++ ] = p[0 ];
97
- p[1 ]-- ;
98
- q. offer(p);
99
- while (q. size() > 1 ) {
100
- p = q. pollFirst();
101
- if (p[1 ] > 0 ) {
102
- pq. offer(p);
103
- }
85
+ Arrays . sort(t, (a, b) - > cnt[a] == cnt[b] ? a - b : cnt[b] - cnt[a]);
86
+ int [] ans = new int [n];
87
+ for (int k = 0 , j = 0 ; k < 2 ; ++ k) {
88
+ for (int i = k; i < n; i += 2 ) {
89
+ ans[i] = t[j++ ];
104
90
}
105
91
}
106
92
return ans;
@@ -111,33 +97,23 @@ class Solution {
111
97
### ** C++**
112
98
113
99
``` cpp
114
- using pii = pair<int , int >;
115
-
116
100
class Solution {
117
101
public:
118
102
vector<int > rearrangeBarcodes(vector<int >& barcodes) {
119
- unordered_map<int, int> cnt;
120
- for (auto& v : barcodes) {
121
- ++cnt[ v] ;
122
- }
123
- priority_queue<pii > pq;
124
- for (auto& [ k, v] : cnt) {
125
- pq.push({v, k});
103
+ int mx = * max_element(barcodes.begin(), barcodes.end());
104
+ int cnt[ mx + 1] ;
105
+ memset(cnt, 0, sizeof(cnt));
106
+ for (int x : barcodes) {
107
+ ++cnt[ x] ;
126
108
}
127
- vector<int > ans;
128
- queue<pii > q;
129
- while (pq.size()) {
130
- auto p = pq.top();
131
- pq.pop();
132
- ans.push_back(p.second);
133
- p.first--;
134
- q.push(p);
135
- while (q.size() > 1) {
136
- p = q.front();
137
- q.pop();
138
- if (p.first) {
139
- pq.push(p);
140
- }
109
+ sort(barcodes.begin(), barcodes.end(), [ &] (int a, int b) {
110
+ return cnt[ a] > cnt[ b] || (cnt[ a] == cnt[ b] && a < b);
111
+ });
112
+ int n = barcodes.size();
113
+ vector<int > ans(n);
114
+ for (int k = 0, j = 0; k < 2; ++k) {
115
+ for (int i = k; i < n; i += 2) {
116
+ ans[ i] = barcodes[ j++] ;
141
117
}
142
118
}
143
119
return ans;
@@ -149,47 +125,58 @@ public:
149
125
150
126
```go
151
127
func rearrangeBarcodes(barcodes []int) []int {
152
- cnt := map[int]int{}
153
- for _, v := range barcodes {
154
- cnt[v]++
128
+ mx := 0
129
+ for _, x := range barcodes {
130
+ mx = max(mx, x)
155
131
}
156
- pq := hp{}
157
- for k, v := range cnt {
158
- heap.Push(&pq, pair{v, k})
132
+ cnt := make([]int, mx+1)
133
+ for _, x := range barcodes {
134
+ cnt[x]++
159
135
}
160
- ans := []int{}
161
- q := []pair{}
162
- for len(pq) > 0 {
163
- p := heap.Pop(&pq).(pair)
164
- v, k := p.v, p.k
165
- ans = append(ans, k)
166
- q = append(q, pair{v - 1, k})
167
- for len(q) > 1 {
168
- p = q[0]
169
- q = q[1:]
170
- if p.v > 0 {
171
- heap.Push(&pq, p)
172
- }
136
+ sort.Slice(barcodes, func(i, j int) bool {
137
+ a, b := barcodes[i], barcodes[j]
138
+ if cnt[a] == cnt[b] {
139
+ return a < b
140
+ }
141
+ return cnt[a] > cnt[b]
142
+ })
143
+ n := len(barcodes)
144
+ ans := make([]int, n)
145
+ for k, j := 0, 0; k < 2; k++ {
146
+ for i := k; i < n; i, j = i+2, j+1 {
147
+ ans[i] = barcodes[j]
173
148
}
174
149
}
175
150
return ans
176
151
}
177
152
178
- type pair struct {
179
- v int
180
- k int
153
+ func max(a, b int) int {
154
+ if a > b {
155
+ return a
156
+ }
157
+ return b
181
158
}
159
+ ```
182
160
183
- type hp []pair
161
+ ### ** TypeScript **
184
162
185
- func (h hp) Len() int { return len(h) }
186
- func (h hp) Less(i, j int) bool {
187
- a, b := h[i], h[j]
188
- return a.v > b.v
163
+ ``` ts
164
+ function rearrangeBarcodes(barcodes : number []): number [] {
165
+ const mx = Math .max (... barcodes );
166
+ const cnt = Array (mx + 1 ).fill (0 );
167
+ for (const x of barcodes ) {
168
+ ++ cnt [x ];
169
+ }
170
+ barcodes .sort ((a , b ) => (cnt [a ] === cnt [b ] ? a - b : cnt [b ] - cnt [a ]));
171
+ const n = barcodes .length ;
172
+ const ans = Array (n );
173
+ for (let k = 0 , j = 0 ; k < 2 ; ++ k ) {
174
+ for (let i = k ; i < n ; i += 2 , ++ j ) {
175
+ ans [i ] = barcodes [j ];
176
+ }
177
+ }
178
+ return ans ;
189
179
}
190
- func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
191
- func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
192
- func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
193
180
```
194
181
195
182
### ** ...**
0 commit comments