Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Distant Barcodes in Python
In a warehouse with a row of barcodes, we need to rearrange them so that no two adjacent barcodes are the same. For example, if we have [1,1,1,2,2,2], we want to output [2,1,2,1,2,1].
The strategy is to place the most frequent barcode first at even positions (0, 2, 4...), then fill odd positions with remaining barcodes.
Algorithm Steps
- Count frequency of each barcode
- Sort barcodes by frequency (ascending order)
- Place the most frequent barcode at even positions (0, 2, 4...)
- Place remaining barcodes at odd positions (1, 3, 5...)
Implementation
class Solution:
def rearrangeBarcodes(self, barcodes):
# Count frequency of each barcode
frequency = {}
for barcode in barcodes:
frequency[barcode] = frequency.get(barcode, 0) + 1
# Create list of [barcode, frequency] pairs and sort by frequency
barcode_freq = [[barcode, freq] for barcode, freq in frequency.items()]
barcode_freq.sort(key=lambda x: x[1])
# Initialize result array
result = [0] * len(barcodes)
i = 0
# Fill even positions (0, 2, 4...) with most frequent barcodes
while i < len(result):
result[i] = barcode_freq[-1][0]
barcode_freq[-1][1] -= 1
if barcode_freq[-1][1] == 0:
barcode_freq.pop()
i += 2
# Fill odd positions (1, 3, 5...) with remaining barcodes
i = 1
while i < len(result):
result[i] = barcode_freq[-1][0]
barcode_freq[-1][1] -= 1
if barcode_freq[-1][1] == 0:
barcode_freq.pop()
i += 2
return result
# Test the solution
solution = Solution()
barcodes = [1, 1, 1, 2, 2, 2]
result = solution.rearrangeBarcodes(barcodes)
print(f"Input: {barcodes}")
print(f"Output: {result}")
Input: [1, 1, 1, 2, 2, 2] Output: [2, 1, 2, 1, 2, 1]
How It Works
The algorithm works by:
- Frequency counting: We count how many times each barcode appears
- Sorting: Sort barcodes by frequency to prioritize the most frequent ones
- Even positions first: Place the most frequent barcode at positions 0, 2, 4... to spread them out maximally
- Odd positions next: Fill remaining positions with other barcodes
Another Example
solution = Solution()
# Test with different input
barcodes = [1, 1, 1, 1, 2, 2, 3, 3]
result = solution.rearrangeBarcodes(barcodes)
print(f"Input: {barcodes}")
print(f"Output: {result}")
# Verify no adjacent duplicates
has_adjacent = any(result[i] == result[i+1] for i in range(len(result)-1))
print(f"Has adjacent duplicates: {has_adjacent}")
Input: [1, 1, 1, 1, 2, 2, 3, 3] Output: [1, 2, 1, 3, 1, 2, 1, 3] Has adjacent duplicates: False
Time Complexity
The time complexity is O(n log k) where n is the length of barcodes and k is the number of unique barcodes. The sorting step dominates the complexity.
Conclusion
This greedy approach ensures no adjacent barcodes are identical by strategically placing the most frequent elements first at even positions, then filling odd positions. The algorithm guarantees a valid rearrangement when one exists.
