当前位置:网站首页>1331. array sequence number conversion - quick sort plus binary search
1331. array sequence number conversion - quick sort plus binary search
2022-06-09 10:08:00 【Mr Gao】
1331. Array number conversion - Quick sort plus binary search
Give you an array of integers arr , Please replace each element in the array with their ordinal number after sorting .
The serial number represents how big an element is . The rules for serial numbers are as follows :
The serial number from 1 Numbered starting .
The bigger an element is , So the bigger the serial number . If two elements are equal , So they have the same serial number .
The serial number of each number should be as small as possible .
Example 1:
Input :arr = [40,10,20,30]
Output :[4,1,2,3]
explain :40 It's the biggest element . 10 It's the smallest element . 20 It's the second smallest number . 30 It's the third smallest number .
Example 2:
Input :arr = [100,100,100]
Output :[1,1,1]
explain : All elements have the same sequence number .
Example 3:
Input :arr = [37,12,28,9,100,56,80,5,12]
Output :[5,3,4,2,8,6,7,1,3]
The solution code is as follows :
/** * Note: The returned array must be malloced, assume caller calls free(). */
void quick(int *a,int low,int high){
int l=low,h=high;
if(low<high){
int p=a[low];
while(low<high){
while(low<high&&a[high]>=p){
high--;
}
a[low]=a[high];
while(low<high&&a[low]<=p){
low++;
}
a[high]=a[low];
}
a[low]=p;
quick(a,l,low-1);
quick(a,low+1,h);
}
}
int find_index(int *a,int low ,int high,int val){
// printf("%d %d ",low,high);
if(low==high){
return low;
}
if(low<high){
int mid=(low+high)/2;
if(a[mid]>val){
return find_index(a,low,mid-1,val);
}
if(a[mid]<val){
return find_index(a,mid+1,high,val);
}
return mid;
}
return low;
}
int* arrayRankTransform(int* arr, int arrSize, int* returnSize){
int *a=(int *)malloc(sizeof(int)*arrSize);
int *r=(int *)malloc(sizeof(int)*arrSize);
* returnSize=arrSize;
if(arrSize==0) return arr;
int i;
for(i=0;i<arrSize;i++){
a[i]=arr[i];
}
quick(a,0,arrSize-1);
r[0]=1;
for(i=1;i<arrSize;i++){
// printf("%d ",a[i]);
if(a[i]>a[i-1]){
r[i]=r[i-1]+1;
}
else{
r[i]=r[i-1];
}
}
for(i=0;i<arrSize;i++){
int index= find_index(a,0,arrSize-1,arr[i]);
arr[i]=r[index];
}
return arr;
}
边栏推荐
- 从零开始实现递归神经网络——【torch学习笔记】
- 【实战技能】Google I/O 2022大会AI/ML给开发者的启发
- MSF practice -- the harm of MySQL null password
- [computer network-19] computer network interview questions
- Openstack explanation (12) -- glance installation and Preliminary Configuration
- 长大后的我们为何贪恋年少?
- openstack详解(十三)——Glance Keystone设置与启动
- Minimum path sum
- 【科技、商业和管理】看剧学创业:《硅谷》第六季第3-5集
- JWT and session
猜你喜欢
![[linear algebra] understand positive definite matrix and semi positive definite matrix](/img/e0/fa9378a7d136f7d9a62ea3934e496d.png)
[linear algebra] understand positive definite matrix and semi positive definite matrix

【新手上路常见问答】非IT企业如何做互联网产品

MSF使用小技巧
![[5 machine learning] the most understandable decision tree in the whole network (with source code attached)](/img/cb/815850c5c6ed2b3c20c8ba34caa7d8.png)
[5 machine learning] the most understandable decision tree in the whole network (with source code attached)

MSF tips

【科技、商业和管理】看剧学创业:《硅谷》第六季第1-2集
![[good book recommendation] popular science book of chip industry: core affairs](/img/92/bcccb7b6db839d0dc8666d2eb82013.jpg)
[good book recommendation] popular science book of chip industry: core affairs

【genius_platform软件平台开发】第一万零一讲:电力项目dz产品windows环境vs2017编译遇到的报错汇总

Machine learning notes - create confusion matrix using scikit learn

- Bean method ‘redisConnectionFactory‘ in ‘JedisConnectionConfiguration‘ not loaded because @Conditi
随机推荐
Webrtc series -- calculate frame rate and frame interval
openstack详解(十八)——Nova服务启动与服务创建
DNMAP架构实现和扫描实战
用好条件访问,远程办公更安全高效
User directory one stop guide
[linear algebra] understand positive definite matrix and semi positive definite matrix
[graph machine learning] Introduction to graph neural network (I) spectral graph theory
fabric-ca介紹,安裝,使用
openstack详解(十五)——openstack Nova节点基本原理
1331. 数组序号转换-快速排序加二分查找
机器学习笔记 - 使用scikit-learn创建混淆矩阵
1340. 跳跃游戏 V-动态规划加dfs
[technology, business and management] drama learning and Entrepreneurship: Silicon Valley Season 6 Episode 3-5
C # introductory series (IX) -- method use
随时随地可访问的 IT 资源构成
openstack详解(十二)——Glance安装与初步配置
LeetCode_ Monotone stack_ Medium_ 739. daily temperature
MSF实战——MySQL空密码的危害
【LeetCode】【牛客】二叉树刷题
MSF模块查找详解