当前位置:网站首页>[C language brush leetcode] 1054. Bar code with equal distance (m)
[C language brush leetcode] 1054. Bar code with equal distance (m)
2022-07-29 07:04:00 【kinbo88】
【
In a warehouse , There is a row of bar codes , Among them the first i Barcodes are barcodes[i].
Please rearrange these barcodes , So that any two adjacent barcodes cannot be equal . You can return any answer that meets this requirement , The answer to this question is guaranteed .
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,2,1,2,1]
Tips :
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000
source : Power button (LeetCode)
link :https://leetcode.cn/problems/distant-barcodes
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
】
1. When you see an array, first consider sorting , Sort by times , People think of hash
2. No repetition , Then put even numbers first , Then put the odd position
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct {
int key;
int val;
UT_hash_handle hh;
} UTHASH;
int Cmp(UTHASH *a, UTHASH *b) {
return b->val - a->val;
}
int* rearrangeBarcodes(int* barcodes, int barcodesSize, int* returnSize){
UTHASH *mymap = NULL;
int i;
int idx = 0;
int *retarr = malloc(sizeof(int) * barcodesSize);
for (i = 0; i < barcodesSize; i++) {
UTHASH *find = NULL;
HASH_FIND_INT(mymap, &barcodes[i], find);
if (find != NULL) {
find->val += 1;
} else {
UTHASH *new = malloc(sizeof(UTHASH));
new->key = barcodes[i];
new->val = 1;
HASH_ADD_INT(mymap, key, new);
}
}
HASH_SORT(mymap, Cmp);
UTHASH *cur, *tmp;
HASH_ITER(hh, mymap, cur, tmp) { // Traverse HASH surface
while (cur->val > 0) { // When the current element is not used up
retarr[idx] = cur->key; // Put the elements in retarr
idx = idx + 2; // idx Jump and put , That is, fill in the even digit first
if (idx >= barcodesSize) { // If idx arrive retarr The tail
idx = 1; // Start filling in odd digits
}
cur->val--; // Put one element at a time , The number of elements minus one
}
}
*returnSize = barcodesSize;
return retarr;
}
/*
1. When you see an array, first consider sorting , Sort by times , People think of hash
2. No repetition , Then put even numbers first , Then put the odd position
*/边栏推荐
- 分享一些你代码更好的小建议,流畅编码提搞效率
- Simulation volume leetcode [normal] 222. number of nodes of complete binary tree
- 说一下 TCP/IP 协议?以及每层的作用?
- Not so simple singleton mode
- MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of
- mysql查询区分大小写
- 王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
- Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
- 王树尧老师运筹学课程笔记 02 高等数学基础
- 怎么会不喜欢呢,CICD中轻松发送邮件
猜你喜欢

Some tips of vim text editor

Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra

IO stream - file - properties

How to write controller layer code gracefully?

SDN topology discovery principle

【flask入门系列】Flask-SQLAlchemy的安装与配置

Flink real time warehouse DWD layer (traffic domain) template code

vscode通过remotessh结合xdebug远程调试php解决方案

MySql基础知识(高频面试题)

JVM之垃圾回收机制(GC)
随机推荐
【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
SDN topology discovery principle
buck电路boot电容短路和断路实测波形
1172. 餐盘栈 有序列表+栈
Simulation volume leetcode [ordinary] 172. Zero after factorial
王树尧老师运筹学课程笔记 06 线性规划与单纯形法(几何意义)
城市花样精~侬好!DESIGN#可视化电台即将开播
CVPR2022Oral专题系列(一):低光增强
量子机器学习中的安全性问题
Flink real-time warehouse DWD layer (order placing multiple tables to realize join operation) template code
vim文本编辑器的一些使用小技巧
谷歌零碎笔记之JWT(草稿)
Talk about tcp/ip protocol? And the role of each layer?
Flink实时仓库-DWD层(kafka-关联mysql的lookup join)模板代码
吴恩达老师机器学习课程笔记 03 线性代数回顾
模拟卷Leetcode【普通】150. 逆波兰表达式求值
数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
模拟卷Leetcode【普通】081. 搜索旋转排序数组 II