当前位置:网站首页>[1331. Array serial number conversion]
[1331. Array serial number conversion]
2022-07-28 20:43:00 【[email protected]】
source : Power button (LeetCode)
describe :
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]
Tips :
- 0 <= arr.length <= 105
- -109 <= arr[i] <= 109
Method : Sort + Hashtable
Ideas
First, save the sorted original array with an array , Then use a hash table to save the sequence number of each element , Finally, replace the elements of the original group with sequence numbers and return .
Code :
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> sortedArr = arr;
sort(sortedArr.begin(), sortedArr.end());
unordered_map<int, int> ranks;
vector<int> ans(arr.size());
for (auto &a : sortedArr) {
if (!ranks.count(a)) {
ranks[a] = ranks.size() + 1;
}
}
for (int i = 0; i < arr.size(); i++) {
ans[i] = ranks[arr[i]];
}
return ans;
}
};
Execution time :100 ms, In all C++ Defeated in submission 39.83% Users of
Memory consumption :38.3 MB, In all C++ Defeated in submission 46.48% Users of
Complexity analysis
Time complexity :O(n × logn), among n Is the input array arr The length of , Sorting costs O(n × logn) Time .
Spatial complexity :O(n). Consumption of ordered array and hash table O(n) Space .
arthor:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281843111871.html
边栏推荐
- js飞入js特效弹窗登录框
- 产品力大幅提升 新款福特探险者发布
- 根据openGauss/MogDB的lwtid查看线程堆栈。
- 上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
- Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]
- Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
- Teach you how to draw a map with ArcGIS [thermal map]
- js图片悬挂样式照片墙js特效
- [pytorch] LSTM neural network
- Who cares about the safety of the battery when it ignites in a collision? SAIC GM has something to say
猜你喜欢
About the title of linking to other pages

Linxu 【基本指令】

LVS+KeepAlived高可用部署实战应用

"When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company

Voice controlled robot based on ROS (II): implementation of upper computer

Read JSON configuration file to realize data-driven testing
![[task03: complex query methods - views, subqueries, functions, etc.]](/img/83/2cad48016199b079aca0251b7b4ee8.png)
[task03: complex query methods - views, subqueries, functions, etc.]
![Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]](/img/33/05620dfff2f6ac67691d20c5256c5b.png)
Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]

One article makes you understand what typescript is

Solutions to the environment created by Anaconda that cannot be displayed in pycharm
随机推荐
Unity package project to vs deploy hololens process summary
Linxu 【权限,粘滞位】
Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
LVM logical volume
平均海拔4000米!我们在世界屋脊建了一朵云
Mongoose condition queries part of the data of the specified attribute
System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
Sorting out problems in interface endpoint testing practice using Supertest
一文了解 Rainbond 云原生应用管理平台
Three steps to teach you unity serial communication
网络各层性能测试
Usage Summary of thymeleaf
C# 读取 CSV文件内数据,导入DataTable后显示
Explain the life cycle function in unity in detail
Solutions to the environment created by Anaconda that cannot be displayed in pycharm
太空射击第16课: 道具(Part 2)
Data mining (data preprocessing) -- Notes
Beautiful blue background form input box style
The product power is greatly improved, and the new Ford Explorer is released
十七年运维老兵万字长文讲透优维低代码~