当前位置:网站首页>[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
*/边栏推荐
猜你喜欢

IO stream - file - properties

微信小程序的反编译

二次元卡通渲染——进阶技巧

buck电路boot和ph引脚实测

线程同步—— 生产者与消费者、龟兔赛跑、双线程打印

2D cartoon rendering - advanced skills

实现改变一段文字的部分颜色效果

DM data guard cluster setup

Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)

vim文本编辑器的一些使用小技巧
随机推荐
Difference between CNAME record and a record
Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
王树尧老师运筹学课程笔记 00 写在前面
量子机器学习中的安全性问题
Ali gave several SQL messages and asked how many tree search operations need to be performed?
Security in quantum machine learning
Leetcode-592: fraction addition and subtraction
聊天机器人有何用处?有何类型?看完这些就明白了!
基于C语言设计的学生成绩排名系统
HJ37 统计每个月兔子的总数 斐波那契数列
Some tips of vim text editor
【C语言刷LeetCode】1054. 距离相等的条形码(M)
Simulation volume leetcode [normal] 081. Search rotation sort array II
The difference between pairs and ipairs
Teacher Wu Enda's machine learning course notes 00 are written in the front
Cesium反射
Hj37 statistics of the total number of rabbits per month Fibonacci series
王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
IDEA中实现Mapper接口到映射文件xml的跳转
Idea cannot find a database solution