Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 752 Bytes

1054.条形码.md

File metadata and controls

41 lines (32 loc) · 752 Bytes

代码

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
};

复杂度分析

时间复杂度 $O(N)$

空间复杂度 $O(M)$ 未重复的数字个数