当前位置:网站首页>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)
边栏推荐
- ESP8266 Arduino OLED
- 57 - II. Continuous positive sequence with sum s
- Sv806 QT UI development
- Qs100 at command mqtt access thingsboard
- Kwai opens a milestone activity for fans to record every achievement moment for creators
- 2022“高考记忆” 已打包完成,请查收!
- According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"
- [backtracking based on bit operation] queen n problem 2
- @Configurationproperties value cannot be injected
- Force/release learning arrangement in IC Verification (5) research on the influence of reg type signals
猜你喜欢

Stm32f4 ll library multi-channel ADC

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

A complete set of installation procedures (for learning and communication only)

Introduction to MMS memory optimization of Hisilicon MPP service

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

asp. Net core theme Middleware

Development of video preview for main interface of pupanvr-ui

Quickly get PCA (principal component analysis) (principle code case)

How to deploy PostgreSQL as a docker container

ShanMeng and Beijing Adoption Day start NFT digital collection public offering
随机推荐
Some optimization methods for UI Application of Qt5 on Hisilicon security platform
Sentinel-2 data introduction and download
Yolov5 realizes road crack detection
Kwai opens a milestone activity for fans to record every achievement moment for creators
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
Map coordinate conversion of Baidu map API
Chapter 1
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
Pupanvr- establishment of development environment and diary, addition of some basic tool functions (3)
31. stack push in and pop-up sequence
One dragon and one knight accompanying programmers are 36 years old
[backtracking based on bit operation] queen n problem 2
Rv1109 lvgl UI development
Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind
Pupanvr- an open source embedded NVR system (1)
@Configurationproperties value cannot be injected
How to deploy PostgreSQL as a docker container
Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
Understanding of day16 array create query static and dynamic array array array performance in memory
WiFi smartconfig implementation