当前位置:网站首页>Sword finger offer II 075 Array relative sort (custom sort, count sort)
Sword finger offer II 075 Array relative sort (custom sort, count sort)
2022-06-30 08:05:00 【Lafiteeee】
The finger of the sword Offer II 075. Array relative sort ( Custom sort 、 Count sorting )
Title Description

Method 1 : The simplest and easiest way to think of
- Use one
map<int, int> mpRecordarr1The number of occurrences of each number in - Traverse
arr2At the same time , To the answer arrayansAdd... In the looparr2[i], The number of cycles isarr1Is the number of times , With a variable at the same timecntRecord the sum of all occurrences . namelymp[arr2[i]], At the end of traversalarr1Appears inarr2The numbers in are in order - Use one
set<int> stStoragearr2Number in , Traversearr1, If it doesn't appear instin , Just add it to the answer arrayansBack ; - Come here ,
ansThe first... In the arraycntThe numbers are already in order , The following numbers are out of order , Just sort the following numberssort(ans.begin() + cnt, ans.end()).
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
map<int, int> mp;
set<int> st;
for (int i = 0; i < arr1.size(); i++) {
mp[arr1[i]]++;
}
vector<int> tmp;
int cnt = 0;
for (auto k : arr2) {
st.insert(k);
int n = mp[k];
cnt += n;
while (n--) {
tmp.push_back(k);
}
}
for (auto num : arr1) {
if (st.find(num) == st.end()) {
tmp.push_back(num);
}
}
sort(tmp.begin() + cnt, tmp.end());
return tmp;
}
};
Method 2 : Directly customize the sorting of arrays
After reading the solution , To sum up . In fact, you can customize the sorting method , Yes arr1 Sort directly .
- Start with a hash table
map<int, int> mpRecordarr2All numbers inarr2[i]And its subscript position[i], That is to saymp[arr2[i]] = [i]; - Customize
sortComparison method in function , For two elementsa、b:- If a、b All in the hash table , explain a、b All in
arr2in , So compare their subscript positions , Elements with small subscripts are smaller ; - If a、b Are not in the hash table , Compare in normal size order ;
- If one is in a hash table , One is not in the hash table , Then the elements in the hash table are smaller .
- If a、b All in the hash table , explain a、b All in
- Direct pair
arr1Just sort it out . - Time complexity : O ( n l o g n ) O(nlogn) O(nlogn) Spatial complexity : O ( m ) O(m) O(m)
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
unordered_map<int, int> mp;
for (int i = 0; i < arr2.size(); i++) {
mp[arr2[i]] = i;
}
sort(arr1.begin(), arr1.end(), [&](const int &a, const int &b) {
if (mp.count(a)) {
return mp.count(b) ? mp[a] < mp[b] : true;
} else {
return mp.count(b) ? false : a < b;
}
});
return arr1;
}
};
Method 3 : Count sorting ( Optimize method 1 )
Consider method one carefully , In fact, it is a bit like counting and sorting . Sort by count , There is no need to use a hash table to store the number of occurrences of an element , Instead, use an array . Topic arr2 The maximum value of elements in the array does not exceed 1000, So you can open a length of 1001 Array to record each number in arr1 Is the number of times , as well as arr2 The elements that appear in .
- Time complexity O ( n + m + 1001 ) O(n+m+1001) O(n+m+1001), Spatial complexity O ( 1001 ) O(1001) O(1001)
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
vector<int> freq(1001);
for (auto num : arr1) {
freq[num]++;
}
vector<int> ans;
for (auto num : arr2) {
while (freq[num]) {
ans.push_back(num);
freq[num]--;
}
}
for (int i = 0; i < freq.size(); i++) {
while (freq[i]) {
ans.push_back(i);
freq[i]--;
}
}
return ans;
}
};
边栏推荐
- 深度学习——Bounding Box预测
- Final review -php learning notes 5-php array
- 【JUC系列】Fork/Join框架之概览
- Examen final - notes d'apprentissage PHP 5 - Tableau PHP
- Deep learning -- feature point detection and target detection
- 深度学习——残差网络ResNets
- 6月底了,可以开始做准备了,不然这么赚钱的行业就没你的份了
- Palindrome substring, palindrome subsequence
- Deep learning -- language model and sequence generation
- 深度学习——词汇表征
猜你喜欢
![November 9, 2020 [wgs/gwas] - whole genome analysis (association analysis) process (Part 2)](/img/21/ad74700921ee0ef7a1525dd7db0683.jpg)
November 9, 2020 [wgs/gwas] - whole genome analysis (association analysis) process (Part 2)
![December 19, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5 advanced database search)](/img/e9/8646f3e2da0ece853e7135eb6e30d9.jpg)
December 19, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5 advanced database search)

冰冰学习笔记:快速排序

深度学习——残差网络ResNets

Deep learning - networks in networks and 1x1 convolution

C # about Net cognition

【Tensorflow-gpu】window11下深度学习环境搭建

深度学习——Bounding Box预测

想转行,却又不知道干什么?此文写给正在迷茫的你

【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
随机推荐
你了解IP协议吗?
牛客小白月賽52
December 4, 2021 [metagenome] - sorting out the progress of metagenome process construction
深度学习——LSTM
【花雕体验】14 行空板pinpong库测试外接传感器模块(之一)
Go 数据类型篇之基本数据类型之间的转化
ACM. Hj48 delete the node with the specified value from the one-way linked list ●●
Getordefault method of map class
深度学习——GRU单元
Deep learning -- recurrent neural network
Deep learning -- language model and sequence generation
深度学习——序列模型and数学符号
Niuke White Moon race 52
Simple application of generating function -- integer splitting 2
Dlib library blink
Summary and common applications of direction and angle operators in Halcon
F12 packet capture is used for the whole process analysis of postman interface test
Miracle Mu server rental selection is real and easy to use, stable and intrusion proof
Cadence innovus physical implementation series (I) Lab 1 preliminary innovus
[flower carving experience] 12 build the Arduino development environment of esp32c3