美文网首页
LeetCode #1054 Distant Barcodes

LeetCode #1054 Distant Barcodes

作者: air_melt | 来源:发表于2022-03-22 23:16 被阅读0次

1054 Distant Barcodes 距离相等的条形码

Description:
In a warehouse, there is a row of barcodes, where the ith barcode is barcodes[i].

Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.

Example:

Example 1:

Input: barcodes = [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]

Example 2:

Input: barcodes = [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,1,2,1,2]

Constraints:

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

题目描述:
在一个仓库里,有一排条形码,其中第 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

思路:

模拟
记录每个元素的出现次数
利用最大堆存储每个元素的出现次数及对应值
按照出现次数每次弹出两个元素插入结果中
然后减少出现次数再插入最大堆中
时间复杂度O(nlgn), 空间复杂度O(1)

代码:
C++:

class Solution 
{
public:
    vector<int> rearrangeBarcodes(vector<int>& barcodes) 
    {
        unordered_map<int, int> m;
        priority_queue<pair<int,int>> pq;
        vector<int> result;
        for (const auto& b : barcodes) ++m[b];
        for (const auto& [k, v] : m) pq.push(make_pair(v, k));
        while(pq.size() >= 2)
        {
            auto top1 = pq.top(); 
            pq.pop();
            auto top2 = pq.top(); 
            pq.pop();
            result.emplace_back(top1.second);
            result.emplace_back(top2.second); 
            if (--top1.first > 0) pq.push(top1);
            if (--top2.first > 0) pq.push(top2);
        }
        if (!pq.empty()) result.emplace_back(pq.top().second);
        return result;
    }
};

Java:

class Solution {
    public int[] rearrangeBarcodes(int[] barcodes) {
        int n = barcodes.length, count[] = new int[10001], cur[] = new int[n], j = 0, result[] = new int[n];
        for (int barcode : barcodes) ++count[barcode];
        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> count[o2] - count[o1]);
        for (int i = 1; i < count.length; i++) if (count[i] != 0) pq.offer(i); 
        while (!pq.isEmpty()) {
            int item = pq.poll();
            for (int i = 0; i < count[item]; i++) cur[j++] = item;
        }
        j = 0; 
        for (int i = 0; i < n; i += 2) result[i] = cur[j++];
        for (int i = 1; i < n; i += 2) result[i] = cur[j++];
        return result;
    }
}

Python:

class Solution:
    def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
        return list(itertools.chain.from_iterable(zip_longest((data := sum([[i] * j for i, j in Counter(barcodes).most_common()], []))[:(((n := len(barcodes)) + 1)) >> 1], data[(n + 1) >> 1:])))[:n]

相关文章

  • LeetCode #1054 Distant Barcodes

    1054 Distant Barcodes 距离相等的条形码 Description:In a warehouse...

  • LeetCode 1054. Distant Barcodes

    参考来源 https://www.acwing.com/solution/LeetCode/content/2389/

  • 泰戈尔爱情诗《世界上最远的距离》

    英文原版: The most distant way in the world The most distant ...

  • distant

    亲爱的Echo,你说的很对。 我现在大概是内心极度匮乏了,我总是看见他,他们,那么远那么远,不是在彼岸的,在彼岸的...

  • UPC

    Barcodes Talk: https://hans.barcodestalk.cn.com/bar-code-...

  • distant place

    you can't leave the world to the people you despise.

  • Distant star

    I touch the wind, Touch the night sky. The distant star, ...

  • Not as distant as it seemed

    “你习惯晚睡 你喜欢发呆 你没什么坚持的动力 你也觉得很难做个开心的人 你无法忍受那样的自己 又深知没有能力改变 ...

  • A distant land

    在那遥远的地方 有位好姑娘 她那粉红的小脸 好像红太阳 她愿做一只小羊 跟在你身旁 在那遥远的地方 有位好姑娘 她...

  • Prepare Barcodes for Demultiplex

    usr/bin/perl; by Mumu use strict;use warnings; chdir "/Us...

网友评论

      本文标题:LeetCode #1054 Distant Barcodes

      本文链接:https://www.haomeiwen.com/subject/ocdfjrtx.html