当前位置:网站首页>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;
}
};
边栏推荐
- MySQL optimizes query speed
- Memcached comprehensive analysis – 5 Memcached applications and compatible programs
- leetcode1863_2021-10-14
- 煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
- Direct attack on "three summers" production: good harvest news spreads frequently and summer broadcasting is in full swing
- [精选] 多账号统一登录,你如何设计?
- 多路转接select
- Guava中这些Map的骚操作,让我的代码量减少了50%
- How to achieve energy conservation and environmental protection of full-color outdoor LED display
- 火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。
猜你喜欢

Datakit 代理实现局域网数据统一汇聚

Pattern recognition - 9 Decision tree

CondaValueError: The target prefix is the base prefix. Aborting.

188. the best time to buy and sell stocks IV

Antdb database online training has started! More flexible, professional and rich

LeetCode-513. 找树左下角的值

(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识

Implementing DNS requester with C language

【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter

VirtualBox virtual machine installation win10 Enterprise Edition
随机推荐
MySQL optimizes query speed
The collection of zero code enterprise application cases in various industries was officially released
[featured] how do you design unified login with multiple accounts?
Understanding openstack network
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
Graduation design of phase 6 of the construction practice camp
【吴恩达笔记】多变量线性回归
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial
(to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model
Unity about conversion between local and world coordinates
That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
Network layer & IP
Multi task model of recommended model: esmm, MMOE
ST表+二分
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
Interpretation of ebpf sockops code
Tournament sort
数据链路层 && 一些其他的协议or技术