LeetCode 题解工作台

距离相等的条形码

在一个仓库里,有一排条形码,其中第 i 个条形码为 barcodes[i] 。 请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。 你可以返回任何满足该要求的答案,此题保证存在答案。 示例 1: 输入: barcodes = [1,1,1,2,2,2] 输出: [2,1,2,1,2,1] …

category

6

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们先用哈希表或数组 统计数组 中各个数出现的次数,然后将 中的数按照它们在 中出现的次数从大到小排序,如果出现次数相同,那么就按照数的大小从小到大排序(确保相同的数相邻)。 接下来,我们创建一个长度为 的答案数组 ,然后遍历排好序的 ,将元素依次填入答案数组的 $0, 2, 4, \cdots$ 等偶数下标位置,然后将剩余元素依次填入答案数组的 $1, 3, 5, \cdots$ 等奇…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

在一个仓库里,有一排条形码,其中第 i 个条形码为 barcodes[i]

请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。 你可以返回任何满足该要求的答案,此题保证存在答案。

 

示例 1:

输入:barcodes = [1,1,1,2,2,2]
输出:[2,1,2,1,2,1]

示例 2:

输入:barcodes = [1,1,1,1,2,2,3,3]
输出:[1,3,1,3,2,1,2,1]

 

提示:

  • 1 <= barcodes.length <= 10000
  • 1 <= barcodes[i] <= 10000
lightbulb

解题思路

方法一:计数 + 排序

我们先用哈希表或数组 cntcnt 统计数组 barcodesbarcodes 中各个数出现的次数,然后将 barcodesbarcodes 中的数按照它们在 cntcnt 中出现的次数从大到小排序,如果出现次数相同,那么就按照数的大小从小到大排序(确保相同的数相邻)。

接下来,我们创建一个长度为 nn 的答案数组 ansans,然后遍历排好序的 barcodesbarcodes,将元素依次填入答案数组的 0,2,4,0, 2, 4, \cdots 等偶数下标位置,然后将剩余元素依次填入答案数组的 1,3,5,1, 3, 5, \cdots 等奇数下标位置即可。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(M)O(M)。其中 nnMM 分别是数组 barcodesbarcodes 的长度以及数组 barcodesbarcodes 中的最大值。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
        cnt = Counter(barcodes)
        barcodes.sort(key=lambda x: (-cnt[x], x))
        n = len(barcodes)
        ans = [0] * len(barcodes)
        ans[::2] = barcodes[: (n + 1) // 2]
        ans[1::2] = barcodes[(n + 1) // 2 :]
        return ans
speed

复杂度分析

指标
时间complexity depends on the method used for selecting barcodes. Using a hash table and heap for selection results in O(n log k) time, where n is the length of the array and k is the number of unique barcodes. Space complexity is O(k) for the storage of frequencies and heap.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates who use a greedy approach with heap selection and hash tables are likely on the right track.

  • question_mark

    Look for candidates who efficiently update barcode counts after placing each element in the sequence.

  • question_mark

    Candidates who rely on brute force without using efficient data structures may struggle with larger inputs.

warning

常见陷阱

外企场景
  • error

    Not updating the barcode frequency after each placement, which can lead to incorrect placements of barcodes.

  • error

    Ignoring edge cases where fewer barcode types are present in the array.

  • error

    Inefficient approaches that do not utilize data structures like heaps or hash tables can lead to slower solutions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Try with arrays containing only one type of barcode to test if the algorithm handles this edge case.

  • arrow_right_alt

    Consider arrays with large numbers of unique barcode types to evaluate the efficiency of the heap-based solution.

  • arrow_right_alt

    Test cases with arrays that require precise placement of barcodes at specific indices.

help

常见问题

外企场景

距离相等的条形码题解:数组·哈希·扫描 | LeetCode #1054 中等