当前位置:网站首页>Leetcode · daily question · 1331. array sequence number conversion · discretization
Leetcode · daily question · 1331. array sequence number conversion · discretization
2022-07-28 13:19:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/rank-transform-of-an-array/solution/by-xun-ge-v-njcw/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
subject

Example

Ideas
The corresponding hash algorithm is not particularly familiar , You can see the link below to learn :
Hash algorithm details
Their thinking
For this question , In fact, it is to discretize the array elements , The meaning of being is , When we encounter an array with a very large range of values , We want to process this array , But in the process of processing, we only care about the size relationship of elements , Don't care about the actual value , However, a very large range of values will affect our use of some good data structures , Such as line segment tree ... At this time, we need to discretize the array elements . For example, array [1,22,333,4444,55555], When we only care about the size of elements , Actually sum [1,2,3,4,5] It is equivalent. .
Concrete realization
First, the array elements are processed in ascending order . Then take the array elements as hashes key value , Save to hash table , And marked , Then traverse the original array , Store the discretization result of the corresponding array in the corresponding position .
Code
typedef struct {// Define hash table
int key;
int val;
UT_hash_handle hh;
} HashItem;
static inline int cmp(const void *pa, const void *pb) {// Ascending subfunction
return *(int *)pa - *(int *)pb;
}
int* arrayRankTransform(int* arr, int arrSize, int* returnSize) {
int *sortedArr = (int *)malloc(sizeof(int) * arrSize);
int *ans = (int *)malloc(sizeof(int) * arrSize);
memcpy(sortedArr, arr, sizeof(int) * arrSize);
qsort(sortedArr, arrSize, sizeof(int), cmp);// Initialize variable
HashItem *ranks = NULL;// Define the hash header node
int n = 1;
for (int i = 0; i < arrSize; i++) {// Traverse the ascending array , And add it to the hash table
HashItem *pEntry = NULL;// Define hash elements
HASH_FIND_INT(ranks, &sortedArr[i], pEntry);// Query whether the element already exists in the hash table
if (pEntry == NULL) {// non-existent , Application node , Save to hash table
pEntry = (HashItem *)malloc(sizeof(HashItem));
pEntry->key = sortedArr[i];
pEntry->val = n++;// Mark
HASH_ADD_INT(ranks, key, pEntry);// Add to hash table
}// It exists because the same elements are marked the same , So there's no need to deal with
}
for (int i = 0; i < arrSize; i++) {// Traverse the original array
HashItem *pEntry = NULL;
HASH_FIND_INT(ranks, &arr[i], pEntry);// Find the tag corresponding to the original array element
ans[i] = pEntry->val;
}
*returnSize = arrSize;
HashItem *cur, *tmp;
HASH_ITER(hh, ranks, cur, tmp) {// Destroy hash table
HASH_DEL(ranks, cur);
free(cur);
}
free(sortedArr);
return ans;
}
author :xun-ge-v
link :https://leetcode.cn/problems/rank-transform-of-an-array/solution/by-xun-ge-v-njcw/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .Time and space complexity

边栏推荐
- Leetcode 笔记 566. 重塑矩阵
- Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
- Pointnet++ Chinese Translation
- Chapter 6 提升
- MySQL 实践篇 —— 主从复制
- [embedded C foundation] Part 3: constants and variables
- [FPGA]: ise and Modelsim joint simulation
- .NET的求复杂类型集合的差集、交集、并集
- [embedded C foundation] Part 7: detailed introduction to C language process control
- Extended operator
猜你喜欢

PCP parity principle arbitrage

Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
![[July 5 event preview] Flink Summit](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[July 5 event preview] Flink Summit
![[FPGA] FIR filter - half band filter](/img/6e/d97b3842f80e37aa41b888384a14cb.png)
[FPGA] FIR filter - half band filter

Black cat takes you to learn EMMC Protocol Part 26: hardware reset operation of EMMC (h/w reset)

Kotlin是如何帮助你避免内存泄漏的?

Definition of option basis

Dimming and color matching cool light touch chip-dlt8ma12ts-jericho

企业数字化本质

Aragon creates Dao polygon BSC test network
随机推荐
Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation
一根筋教育PHP培训 知行合一收热捧
SSM框架网上书城全套
How much do you know about JVM memory management
《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
[embedded C foundation] Part 2: binary conversion and BCD coding
Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
MySQL 实践篇 —— 主从复制
Redis - Basics
Sub thread update UI full solution
【嵌入式C基础】第5篇:原码/反码/补码
leetcdoe-342. 4的幂
【嵌入式C基础】第3篇:常量和变量
BiliBili Yang Zhou: above efficiency, efficient delivery
Why neural networks are ineffective?
MySQL practice -- master-slave replication
IP电话系统和VoIP系统使用指南
[FPGA]: ISE generates MCS file and burning process
Kotlin是如何帮助你避免内存泄漏的?
Solution to using json.tojsonstring to display question marks in Chinese in Servlet