当前位置:网站首页>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

边栏推荐
- BiliBili Yang Zhou: above efficiency, efficient delivery
- 一根筋教育PHP培训 知行合一收热捧
- Li FuPan: application practice of kata safety container in ant group
- 【嵌入式C基础】第8篇:C语言数组讲解
- Jetpack Compose 完全脱离 View 系统了吗?
- 【嵌入式C基础】第6篇:超详细的常用的输入输出函数讲解
- [June 28 event preview] low code Summit
- 子线程更新UI全解
- Leetcode 笔记 118. 杨辉三角
- LeetCode每日一题(2196. Create Binary Tree From Descriptions)
猜你喜欢
![[July 5 event preview] Flink Summit](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[July 5 event preview] Flink Summit

夜神模拟器抓包微信小程序

拥有游戏的一部分,写在我的世界禁用NFT之后

LeetCode每日一题(2196. Create Binary Tree From Descriptions)

RGB game atmosphere light touch chip-dlt8s04a-jericho

Aragon创建DAO polygon BSC测试网

Change password, confirm password verification antd

Fast classification of array.group() in ES6

2020-12-13

With 433 remote control UV lamp touch chip-dlt8sa20a-jericho
随机推荐
JVM 内存管理 你知道多少
Understanding of vite2
Low code: reduce technical capability requirements and improve software development efficiency
The difference between sessionstorage, localstorage and cookies
Sub thread update UI full solution
[embedded C foundation] Part 9: basic usage of C language pointer
[embedded C foundation] Part 5: original code / inverse code / complement code
MySQL 实践篇 —— 主从复制
CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
Fast classification of array.group() in ES6
8、 Kubernetes network and load balancing
PCP parity principle arbitrage
企业数字化本质
Definition of option basis
gicv3 spi register
[basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things
Protobuf data exchange format
QT signal and slot mechanism (detailed)
[embedded C foundation] Part 2: binary conversion and BCD coding
Redis - Basics