-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistant Barcodes.cpp
41 lines (31 loc) · 1013 Bytes
/
Distant Barcodes.cpp
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
30
31
32
33
34
35
36
37
38
class Solution {
public:
vector<int> rearrangeBarcodes(vector<int>& barcodes) {
int maxFreq=0;
vector<int> count(10001, 0);
for(int i=0 ; i<barcodes.size() ; i++) {
count[barcodes[i]]++;
if(count[barcodes[i]] > count[maxFreq]) {
maxFreq = barcodes[i];
}
}
int i=0 ;
while(count[maxFreq]) {
barcodes[i] = maxFreq;
count[maxFreq]--;
i+=2;
}
for(int j=1 ; j<10001 ; j++) {
while(count[j]) {
if(i>=barcodes.size()) i=1;
barcodes[i] = j;
i+=2;
count[j]--;
}
}
return barcodes;
}
};
// LEETCODE 1054
// In a warehouse, there is a row of barcodes, where the ith 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.