var rearrangeBarcodes = function(barcodes) {
const len = barcodes.length
const map = new Map()
for (let i = 0; i < len; i++) {
const target = barcodes[i]
map.set(target, (map.get(target) || 0) + 1)
}
const arr = [...map.entries()].sort((a, b) => a[1]-b[1])
let res = []
let even = 0
let odd = 1
while (arr.length) {
let [value, count] = arr.pop()
while (count && even < len) {
res[even] = value
even += 2
count--
}
while (count && odd < len) {
res[odd] = value
odd += 2
count--
}
}
return res
};
时间复杂度
空间复杂度