当前位置:网站首页>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;
}
};
边栏推荐
- 【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
- Multi view function in blender
- Blender's simple skills - array, rotation, array and curve
- 面试官:你说你精通Redis,你看过持久化的配置吗?
- Bld3 getting started UI
- 《各行业零代码企业应用案例集锦》正式发布
- 【Camera基础(一)】Camera摄像头工作原理及整机架构
- Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
- openGauss内核:简单查询的执行
- 02---纵波不可能产生的现象
猜你喜欢

【吴恩达笔记】多变量线性回归

AntDB数据库在线培训开课啦!更灵活、更专业、更丰富

多路转接select

Multi view function in blender

The collection of zero code enterprise application cases in various industries was officially released

Docking of arkit and character creator animation curves

如何做到全彩户外LED显示屏节能环保

Kubernetes 集群中流量暴露的几种方案

多线程收尾

Vscode netless environment rapid migration development environment (VIP collection version)
随机推荐
如何做到全彩户外LED显示屏节能环保
Intelligent fish tank control system based on STM32 under Internet of things
socket(2)
leetcode:1504. 统计全 1 子矩形的个数
Volcano成Spark默认batch调度器
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
Antdb database online training has started! More flexible, professional and rich
火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。
Blender's landscape
煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
Object.defineProperty和Reflect.defineProperty的容错问题
二叉搜索树模板
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
Vscode netless environment rapid migration development environment (VIP collection version)
02--- impossible phenomenon of longitudinal wave
Network layer & IP
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
LeetCode-513. 找树左下角的值
ST表+二分
【吴恩达笔记】多变量线性回归