目录
- 1.排序数组
- 2.数组中的逆序对
- 3.计算右侧小于当前元素的个数
- 4.翻转对
1.排序数组
排序数组
class Solution {
int tmp[50010];
public:
vector<int> sortArray(vector<int>& nums) {
mergeSort(nums,0,nums.size()-1);
return nums;
}
void mergeSort(vector<int>& nums,int left,int right)
{
if(left>=right) return;
int mid = (left+right)>>1;
mergeSort(nums,left,mid);
mergeSort(nums,mid+1,right);
int cur1 = left,cur2 = mid+1,i=0;
while(cur1<=mid && cur2<=right)
{
tmp[i++] = nums[cur1]>=nums[cur2]?nums[cur2++]:nums[cur1++];
}
while(cur1<=mid) tmp[i++] = nums[cur1++];
while(cur2<=right) tmp[i++] = nums[cur2++];
for(int j=left;j<=right;j++)
{
nums[j] = tmp[j-left];
}
}
};
2.数组中的逆序对
数组中的逆序对
class Solution {
int tmp[50010];
public:
int reversePairs(vector<int>& nums) {
return mergeSort(nums,0,nums.size()-1);
}
int mergeSort(vector<int>& nums,int left,int right)
{
if(left>=right) return 0;
int ret = 0;
int mid = (left+right)>>1;
ret += mergeSort(nums,left,mid);
ret += mergeSort(nums,mid+1,right);
int cur1 = left,cur2 = mid+1,i=0;
while(cur1<=mid&& cur2<=right)
{
if(nums[cur1]<=nums[cur2]) tmp[i++] = nums[cur1++];
else
{
ret += mid-cur1+1;
tmp[i++] = nums[cur2++];
}
}
while(cur1<=mid) tmp[i++] = nums[cur1++];
while(cur2<=right) tmp[i++] = nums[cur2++];
for(int j=left;j<=right;j++)
{
nums[j] = tmp[j-left];
}
return ret;
}
};
3.计算右侧小于当前元素的个数
计算右侧小于当前元素的个数
class Solution {
vector<int> ret;
vector<int> index;
int tmpNums[500010];
int tmpIndex[500010];
public:
vector<int> countSmaller(vector<int>& nums) {
int n = nums.size();
ret.resize(n);
index.resize(n);
for(int i=0;i<n;i++)
{
index[i] = i;
}
mergeSort(nums,0,n-1);
return ret;
}
void mergeSort(vector<int>& nums,int left,int right)
{
if(left>=right) return;
int mid = (left+right)>>1;
mergeSort(nums,left,mid);
mergeSort(nums,mid+1,right);
int cur1 = left,cur2 = mid+1,i=0;
while(cur1<=mid&& cur2<=right)
{
if(nums[cur1]<=nums[cur2])
{
tmpNums[i] = nums[cur2];
tmpIndex[i++] = index[cur2++];
}
else
{
ret[index[cur1]] += right-cur2+1;
tmpNums[i] = nums[cur1];
tmpIndex[i++] = index[cur1++];
}
}
while(cur1<=mid)
{
tmpNums[i] = nums[cur1];
tmpIndex[i++] = index[cur1++];
}
while(cur2<=right)
{
tmpNums[i] = nums[cur2];
tmpIndex[i++] = index[cur2++];
}
for(int j=left;j<=right;j++)
{
nums[j] = tmpNums[j-left];
index[j] = tmpIndex[j-left];
}
}
};
4.翻转对
翻转对
class Solution {
int tmp[50010];
public:
int reversePairs(vector<int>& nums) {
return mergeSort(nums,0,nums.size()-1);
}
int mergeSort(vector<int>& nums,int left,int right)
{
if(left>=right) return 0;
int ret = 0;
int mid = (left+right)>>1;
ret += mergeSort(nums,left,mid);
ret += mergeSort(nums,mid+1,right);
int cur1 = left,cur2 = mid+1,i=0;
while(cur2<=right)
{
while(cur1<=mid && nums[cur1]/2.0<=nums[cur2]) cur1++;
if(cur1 > mid)
break;
ret += mid-cur1+1;
cur2++;
}
cur1 = left,cur2 = mid+1;
while(cur1<=mid && cur2<=right)
{
tmp[i++] = nums[cur1]<=nums[cur2]?nums[cur1++]:nums[cur2++];
}
while(cur1<=mid) tmp[i++] = nums[cur1++];
while(cur2<=right) tmp[i++] = nums[cur2++];
for(int j=left;j<=right;j++)
{
nums[j] = tmp[j-left];
}
return ret;
}
};
class Solution {
int tmp[50010];
public:
int reversePairs(vector<int>& nums) {
return mergeSort(nums,0,nums.size()-1);
}
int mergeSort(vector<int>& nums,int left,int right)
{
if(left>=right) return 0;
int ret = 0;
int mid = (left+right)>>1;
ret += mergeSort(nums,left,mid);
ret += mergeSort(nums,mid+1,right);
int cur1 = left,cur2 = mid+1,i=0;
while(cur1<=mid)
{
while(cur2<=right && nums[cur1]/2.0<=nums[cur2]) cur2++;
if(cur2 > right)
break;
ret +=right-cur2+1;
cur1++;
}
cur1 = left,cur2 = mid+1;
while(cur1<=mid && cur2<=right)
{
tmp[i++] = nums[cur1]<=nums[cur2]?nums[cur2++]:nums[cur1++];
}
while(cur1<=mid) tmp[i++] = nums[cur1++];
while(cur2<=right) tmp[i++] = nums[cur2++];
for(int j=left;j<=right;j++)
{
nums[j] = tmp[j-left];
}
return ret;
}
};