网站空间可以自己做服务器,贵阳网站推广,wordpress 图片 视频,做网站怎么租个域名1. 题目
在一个仓库里#xff0c;有一排条形码#xff0c;其中第 i 个条形码为 barcodes[i]。 请你重新排列这些条形码#xff0c;使其中两个相邻的条形码 不能 相等。 你可以返回任何满足该要求的答案#xff0c;此题保证存在答案。
示例 1#xff1a;
输入#xff1a…1. 题目
在一个仓库里有一排条形码其中第 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来源力扣LeetCode 链接https://leetcode-cn.com/problems/distant-barcodes 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 解题
对数字计数插入优先队列数量多的先出队从0开始隔一个插入一个然后从1开始插空
class Solution {struct cmp{bool operator()(pairint,int a, pairint,int b){return a.second b.second;}//小就是大顶堆};
public:vectorint rearrangeBarcodes(vectorint barcodes) {if(barcodes.size() 2)return barcodes;int n barcodes.size(), i 0, tpnum, tpcount;bool reachEnd false;unordered_mapint,int m;for(auto b : barcodes)m[b];//计数priority_queuepairint,int, vectorpairint,int,cmp q;for(auto mi : m)q.push(mi);vectorint ans(n,0);while(!q.empty()){tpnum q.top().first;tpcount q.top().second;q.pop();while(i n !reachEnd tpcount){while(i n tpcount){ans[i] tpnum;tpcount--;i 2;}if(i n){reachEnd true;//到达末尾了i 1;//填写偶数位}}while(i n tpcount){ans[i] tpnum;tpcount--;i 2;}}return ans;}
};