当前位置:网站首页>Force buckle 315 calculates the number of elements smaller than the current element on the right
Force buckle 315 calculates the number of elements smaller than the current element on the right
2022-07-28 12:48:00 【holdtao】
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int>index,temp,tempindex,counts;
index.resize(nums.size(),0);
temp.resize(nums.size(),0);
tempindex.resize(nums.size(),0);
counts.resize(nums.size(),0);// Array of subscripts that need to be recorded , Used to save subscripts . Use merge sort algorithm to solve
for(int i=0;i<nums.size();i++)index[i]=i;
sort(nums,0,nums.size()-1,index,temp,tempindex,counts);
return counts;
}
void sort(vector<int>&nums,int l,int r,vector<int>&index,vector<int>&temp,vector<int>&tempindex,vector<int>&counts){
if(r==l)return;
int mid = l + (r-l)/2;
sort(nums,l,mid,index,temp,tempindex,counts);
sort(nums,mid+1,r,index,temp,tempindex,counts);
mergersort(nums,l,mid,r,index,temp,tempindex,counts);
}
void mergersort(vector<int>&nums,int l,int mid,int r,vector<int>&index,vector<int>&temp,vector<int>&tempindex,vector<int>&counts){
int left = l,right=mid+1,cnt=0;
while(left<=mid&&right<=r){
if(nums[left]<=nums[right]){
temp[cnt]=nums[left];
tempindex[cnt]=index[left];
counts[index[left]]+=right-mid-1;
cnt++;
left++;
}
else{
temp[cnt] = nums[right];
tempindex[cnt]=index[right];
cnt++;
right++;
}
}
while(left<=mid){
temp[cnt]=nums[left];
tempindex[cnt]=index[left];
counts[index[left]]+=right-mid-1;
cnt++;
left++;
}
while(right<=r){
temp[cnt]=nums[right];
tempindex[cnt]=index[right];
right++;
cnt++;
}
for(int n=0;n<r-l+1;n++){
nums[n+l]=temp[n];
index[n+l]=tempindex[n];
}
}
};边栏推荐
- Zadig v1.13.0 believes in the power of openness, and workflow connects all values
- 机器学习实战-神经网络-21
- SuperMap iclient3d for webgl to realize floating thermal map
- 1331. 数组序号转换 : 简单模拟题
- 西门子对接Leuze BPS_304i 笔记
- 20220728 common methods of object class
- 新零售电商O2O模式解析
- FlexPro软件:生产、研究和开发中的测量数据分析
- Using dependent packages to directly implement paging and SQL statements
- IO流再回顾,深入理解序列化和反序列化
猜你喜欢

Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production

Merge sort

leetcode:704二分查找

Siemens docking Leuze BPS_ 304i notes

LeetCode84 柱状图中最大的矩形

【萌新解题】爬楼梯

开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开

Leetcode: array

Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders

Leetcode94. Middle order traversal of binary trees
随机推荐
The usage and Simulation Implementation of vector in STL
[base] what is the optimization of optimization performance?
公司在什么情况下可以开除员工
Deployment之滚动更新策略。
图书馆自动预约脚本
Using JSON in C language projects
Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
leetcode:数组
归并排序
STM32 loopback structure receives and processes serial port data
VS1003 debugging routine
mysql limit 分页优化
Unity 安装 Device Simulator
Fastjson parses multi-level JSON strings
软件架构师必需要了解的 saas 架构设计?
[cute new problem solving] climb stairs
Sliding Window
[Nuxt 3] (十二) 项目目录结构 3
Analysis of new retail e-commerce o2o model
新零售电商O2O模式解析