-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistant-barcodes.cpp
More file actions
51 lines (44 loc) · 1.27 KB
/
distant-barcodes.cpp
File metadata and controls
51 lines (44 loc) · 1.27 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public:
vector<int> rearrangeBarcodes(vector<int>& barcodes) {
unordered_map<int,int> ww;
for(const auto &b:barcodes) ww[b]++;
vector<pair<int,int>> um; // 个数:索引
for(const auto &w:ww){
if(w.second!=0){
um.push_back({w.second,w.first});
}
}
auto cmp=[](pair<int,int> &a, pair<int,int> &b){
return a.first<b.first;
};
sort(um.begin(),um.end(),cmp);
for(const auto &u:um){
cout<<u.second<<" "<<u.first<<endl;
}
// 按组处理每一个数,只要把它们隔一个空处理就好
vector<int> res(barcodes.size());
int j=um.size()-1;
for(int i=0;i<res.size();i+=2){
if(um[j].first!=0){
res[i]=um[j].second;
um[j].first--;
}
else{
i-=2;
j--;
}
}
for(int i=1;i<res.size();i+=2){
if(um[j].first!=0){
res[i]=um[j].second;
um[j].first--;
}
else{
i-=2;
j--;
}
}
return res;
}
};