← Home

1054. Distant Barcodes

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
heap / priority queueC++Markdown
105

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1054. Distant Barcodes, the solution in this repository is mainly a heap / priority queue solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: heap / priority queue.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are rearrangeBarcodes.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The queue gives the solution a level-by-level or frontier-style traversal.
  • The heap keeps the best candidate available without sorting the whole world every time.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 356 ms, faster than 10.71% of C++ online submissions for Distant Barcodes.
02//Memory Usage: 20.7 MB, less than 100.00% of C++ online submissions for Distant Barcodes.
03class Solution {
04public:
05    vector<int> rearrangeBarcodes(vector<int>& barcodes) {
06        unordered_map<int, int> counter;
07        for(int barcode : barcodes){
08            counter[barcode]++;
09        }
10        
11        //the larger count, the earlier to be popped
12        priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>> pq;
13        
14        //now the pair becomes (count, barcode)
15        for(auto it = counter.begin(); it != counter.end(); it++){
16            pq.push(make_pair(it->second, it->first));
17        }
18        
19        vector<int> ans;
20        
21        while(pq.size() >= 2){
22            pair<int, int> a = pq.top(); pq.pop();
23            pair<int, int> b = pq.top(); pq.pop();
24            
25            //second : barcode
26            ans.push_back(a.second);
27            ans.push_back(b.second);
28            
29            //first : count
30            a.first--;
31            b.first--;
32            
33            if(a.first > 0) pq.push(a);
34            if(b.first > 0) pq.push(b);
35        }
36        
37        if(pq.size() > 0){
38            pair<int, int> a = pq.top(); pq.pop();
39            ans.push_back(a.second);
40        }
41        
42        return ans;
43    }
44};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.