fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. vector<int> rearrangeBarcodes(vector<int>& barcodes) {
  11. unordered_map<int, int> counts;
  12. for (const auto& b : barcodes) {
  13. ++counts[b];
  14. }
  15.  
  16. struct KV {
  17. int code;
  18. int count;
  19. bool operator < (const KV& other) const {
  20. if (count == other.count) {
  21. return code < other.code;
  22. }
  23. return count < other.count;
  24. }
  25. };
  26.  
  27. set<KV> s;
  28. for (const auto& kv : counts) {
  29. s.insert({kv.first, kv.second});
  30. }
  31.  
  32. vector<int> res;
  33. res.reserve(barcodes.size());
  34. while (s.size() > 0) {
  35. auto kv_min = *(prev(s.end()));
  36. s.erase(prev(s.end()));
  37.  
  38.  
  39. }
  40. return res;
  41. }
  42. };
  43.  
  44. int main() {
  45. // your code goes here
  46. return 0;
  47. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Standard output is empty