当前位置:网站首页>leetcode_ one thousand three hundred and sixty-five
leetcode_ one thousand three hundred and sixty-five
2022-06-24 21:53:00 【Programming rookie】
leetcode_1365 How many numbers are smaller than the current number
: Law 1
Utilization conditions each nums[i] <= 100, You can create a 101 An array of frequencies in space , Use counting sorting to calculate the frequency of each number . Then the number of digits less than the current number is the sum of all the preceding items in the frequency array with this number as the subscript .
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int> cnt(101);
for(int e : nums){
// Frequency array
cnt[e]++;
}
int sum = 0;
vector<int> smallerThatVector(101);
for(int i = 0; i < 101; ++i){
smallerThatVector[i] += sum; // Figure out the first few items and the array
sum += cnt[i];
}
for(int i = 0; i < nums.size(); ++i){
nums[i] = smallerThatVector[nums[i]]; // And the items of the array are the answer
}
return nums;
}
};
Law two :
utilize map Automatic sorting of . Create a tree map,key yes int,value yes vector. take nums[i] For each element in the map in , And will nums[i] Insert the corresponding in the table below vector in . So go through it again map,ans The corresponding item of the array is map Medium vector The first few are size and .
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
map<int, vector<int>> cnt; // Every mistake ,map Of value yes vector<int>
for(int i = 0; i < nums.size(); ++i){
cnt[nums[i]].push_back(i); // take nums[i] All insert Get into map
} // And will nums[i] The corresponding subscript inserts the corresponding vector<int>
vector<int> ans(nums.size()); // Result array
int sum = 0;
for(auto it = cnt.begin(); it != cnt.end(); ++it){
int size = (it->second).size();
for(int i = 0; i < size; ++i){
int index = (it->second)[i]; // Get vector<int> Value , That is the
ans[index] = sum; //key In the subscript corresponding to the original array .
}
sum += (it->second).size(); //sum Is all less than the current key The number of and
}
return ans;
}
};
边栏推荐
猜你喜欢
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
2022 international women engineers' Day: Dyson design award shows women's design strength
滤波数据分析
使用region折叠代码
多线程收尾
Why are life science enterprises on the cloud in succession?
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
网络层 && IP
A deep learning model for urban traffic flow prediction with traffic events mined from twitter
随机推荐
心楼:华为运动健康的七年筑造之旅
leetcode1720_2021-10-14
TypeScript快速入门
socket(2)
how to install clustershell
Introduce the overall process of bootloader, PM, kernel and system startup
Antdb database online training has started! More flexible, professional and rich
STL+树
Data link layer & some other protocols or technologies
为什么生命科学企业都在陆续上云?
Graduation summary of phase 6 of the construction practice camp
基于 KubeSphere 的分级管理实践
旅行商问题(TSP)的相关论文总结
最大流问题
Several classes of manual transactions
leetcode1863_2021-10-14
How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
Graduation design of phase 6 of the construction practice camp
02--- impossible phenomenon of longitudinal wave
火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。