当前位置:网站首页>51. reverse order pairs in the array
51. reverse order pairs in the array
2022-06-12 05:20:00 【Be your goat】
The finger of the sword Offer 51. Reverse pairs in arrays
Ideas : Merge sort
- Termination conditions :l>=r when , return 0
- Recursive partition : Calculate the midpoint of the array m, Recursively divide the left subarray and the right subarray ;
- Merge and reverse pair Statistics :
- Temporary array nums Closed interval [l,r] To auxiliary array tmp
- Cyclic merger : Set the double pointers to point to the first element of the right sub array respectively :
- When i==m+1 when , Represents that the left sub array has been merged , Add right subarray , And implement j++
- otherwise , When j==r+1 perhaps tmp[i]<=tmp[j] when , Represents that the right sub array has been merged or the current element of the left sub array is smaller than the current element of the right sub array , Add the left sub array and execute i++
- otherwise , When tmp[i]>tmp[j] when , Represents that the current element of the left sub array is greater than the current element of the right sub array , Add the right sub array and execute j++, At this time, the reverse order pair is formed m-i+1, Add to res
- Merge and reverse pair Statistics :
- return res
class Solution {
public:
int reversePairs(vector<int>& nums) {
vector<int> tmp(nums.size());
int res=mergeSort(0,nums.size()-1,nums,tmp);
return res;
}
int mergeSort(int l,int r,vector<int>& nums,vector<int>& tmp){
if(l>=r) return 0;
int m=l+r>>1;
int res=mergeSort(l,m,nums,tmp)+mergeSort(m+1,r,nums,tmp);
for(int i=l;i<=r;++i) tmp[i]=nums[i];
int i=l,j=m+1,k=l;
while(k<=r){
if(i==m+1)
nums[k++]=tmp[j++];
else if(j==r+1||tmp[i]<=tmp[j]){
nums[k++]=tmp[i++];
}
else{
nums[k++]=tmp[j++];
res+=m-i+1;
}
}
return res;
}
};
Time complexity O(nlogn)
Spatial complexity O(n)
边栏推荐
- Alsa architecture application aplay and amixer call relationship (from application layer to kernel driver)
- Qs100 at command mqtt access thingsboard
- Some optimization methods for UI Application of Qt5 on Hisilicon security platform
- JS how to get the date
- [GIS tutorial] land use transfer matrix
- How to clear floating, and how does it work?
- Font conversion optimization
- Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ already exists; SQL statement:
- JS to determine whether the tags of multiple classes are empty
- Ten trends of Internet Security in 2022 industry released
猜你喜欢

Ten trends of Internet Security in 2022 industry released

Thingsboard create RCP widget

Enhanced vegetation index evi, NDVI data, NPP data, GPP data, land use data, vegetation type data, rainfall data

Ubunt 20.04 uses CDROM or ISO as the installation source

The combined application of TOPSIS and fuzzy borde (taking the second Dawan District cup and the national championship as examples, it may cause misunderstanding, and the Dawan District cup will be up

Some problems of Qinglong panel

4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)

Understanding of day16 array create query static and dynamic array array array performance in memory

Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort

It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application
随机推荐
Why is Julia so popular?
One dragon and one knight accompanying programmers are 36 years old
@Configurationproperties value cannot be injected
How to generate provincial data from county-level data in ArcGIS?
What is thinking
C asynchronous programming (async and await) and asynchronous method synchronous invocation
According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"
Some optimization methods for UI Application of Qt5 on Hisilicon security platform
2022“高考记忆” 已打包完成,请查收!
Day18 creation and restoration of sparse array
Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
Harris corner detection principle-
Kwai opens a milestone activity for fans to record every achievement moment for creators
When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path
62. the last number left in the circle
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e
Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
1006 next spread
How to clear floating, and how does it work?