当前位置:网站首页>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];
}
}
};边栏推荐
- Redis实现分布式锁
- Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
- The usage and Simulation Implementation of vector in STL
- 云原生—运行时环境
- 一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
- 与元素类型 “item” 相关联的 “name” 属性值不能包含'&lt;” 字符解决办法
- How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
- 04 pyechars 地理图表(示例代码+效果图)
- 新零售电商O2O模式解析
- [cute new problem solving] climb stairs
猜你喜欢

GMT installation and use

What SaaS architecture design does a software architect need to know?

单调栈Monotonic Stack

03 pyechars 直角坐标系图表(示例代码+效果图)

Cloud native - runtime environment

Library automatic reservation script

Leetcode 42. rainwater connection

sqli-labs(less-8)

30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held

Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment
随机推荐
Cloud native - runtime environment
通过Jenkins 拉取服务器代码 权限不足问题及其他注意事项
利用依赖包直接实现分页、SQL语句
Merge sort
VS1003调试例程
MarkDown简明语法手册
Initialization examples of several modes of mma8452q
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
[Nuxt 3] (十二) 项目目录结构 3
Multi Chain and multi currency wallet system development cross chain technology
单调栈Monotonic Stack
牛客网二叉树题解
Leetcode394 string decoding
Leetcode: array
30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held
合并表格行---三层for循环遍历数据
scala 转换、过滤、分组、排序
HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
The usage and Simulation Implementation of vector in STL