Day 90 1054. 距离相等的条形码

1054. 距离相等的条形码

题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
在一个仓库里,有一排条形码,其中第 i 个条形码为 barcodes[i]。

请你重新排列这些条形码,使其中两个相邻的条形码 不能相等。

你可以返回任何满足该要求的答案,此题保证存在答案。

 

示例 1

输入:[1,1,1,2,2,2]
输出:[2,1,2,1,2,1]
示例 2

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

提示:

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

题目思路

  • 1、每次取最大堆中的两个对子,然后进行间隔插入即可.
  • 2、最后两日了,明天打卡 91 日后,要考试了,所以停止更新。直到 12 月 30 日开始,重头复盘 91 日,每日复盘 4 题左右,顺便开始阅读相应的论文,并展开研究型的工作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
vector<int> rearrangeBarcodes(vector<int>& barcodes) {
int n = barcodes.size();
unordered_map<int, int> up;
for(int i : barcodes) up[i]++;
vector<pair<int,int>> ans;
for(auto [x, fx] : up) ans.push_back(pair<int, int>{x, fx});

sort(ans.begin(), ans.end(),
[&](const pair<int, int> &a, const pair<int, int> &b)
{
return a.second > b.second;
});

vector<int> res(n);
int m = 0;
for(auto [x, fx] : ans)
{
for (int i = 0 ; i < fx; i++)
{
if (m >= n) m = 1;
res[m] = x;
m += 2;
}
}
return res;
}
};

复杂度

  • 时间复杂度:O(nlogn)
  • 空间复杂度:O(n)

Day 90 1054. 距离相等的条形码
https://chaggle.github.io/2021/12/08/Leetcode/91-day/day-90/
作者
chaggle
发布于
2021年12月8日
许可协议