当前位置:网站首页>Li Kou sword finger offer 51. reverse order pairs in the array
Li Kou sword finger offer 51. reverse order pairs in the array
2022-07-28 13:36:00 【Three watch ghost】
This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together .
Title source :https://leetcode.cn/problems/shu-zu-zhong-de-ni-xu-dui-lcof/
General meaning :
Given an array , Find the number of reverse pairs in the array , The reverse pair is defined as :
- Equipped with i < j, if nums[i] > nums[j] be nums[i] And nums[j] Form an inverse pair of
Ideas
If there are two ascending arrays nums1 and nums2, So how to compare the number of reverse pairs after the combination of these two ascending arrays ?
- First , Because the two arrays themselves are ascending arrays , So there is no reverse order pair in the array itself
- that , The number of reverse pairs after the combination of these two ascending arrays depends on how many elements in the previous array are larger than the latter array
Use double pointer i and j Respectively represent the traversal positions of two ascending arrays
if nums1[i] Greater than nums2[j], that i To the end of the previous array, all elements are greater than nums2[j], These elements are associated with nums2[j] Form an inverse pair of , Count the number and move the pointer back j;
if nums1[i] No more than nums2[j], So the pointer i Move backward
Through the above method, the number of reverse pairs of two ascending arrays can be counted , What if the two arrays are not in ascending order ?
That is, if there are two arrays nums1 and nums2, So how to compare the number of reverse pairs after the combination of these two arrays ?
- First , Statistics nums1 and nums2 The number of pairs in reverse order , Then sort the two arrays
- Calculate the number of reverse pairs after the combination of two ascending arrays according to the above method
So we can use the idea of merging and sorting , Calculate the number of pairs in reverse order while merging and sorting
See code for details :
public int reversePairs(int[] nums) {
int n = nums.length;
return merge(nums, new int[n], 0, n - 1);
}
/** * Merge sort , return [left, right] The number of inverse pairs in the interval * @param nums * @param temp * @param left * @param right * @return */
public int merge(int[] nums, int[] temp, int left, int right) {
if (left >= right) {
return 0;
}
int mid = (left + right) >> 1;
// Merge sort left half
int leftCount = merge(nums, temp, left, mid);
// Merge and sort the right half
int rightCount = merge(nums, temp, mid + 1, right);
// The largest element in the left half is not greater than the smallest element in the right half , It is proved that there will be no new reverse order pairs after the merging of two intervals
if (nums[mid] <= nums[mid + 1]) {
return leftCount + rightCount;
}
// Merge the left and right intervals and calculate the reverse order pair generated by the merge
int crossCOunt = mergeAndCount(nums, temp, left, right);
return leftCount + rightCount + crossCOunt;
}
/** * Combine the two intervals and calculate the reverse order pair generated by the combination * * @param nums * @param temp * @param left * @param right * @return */
public int mergeAndCount(int[] nums, int[] temp, int left, int right) {
// First, copy the merged interval elements to the temporary array
for (int i = left; i <= right; i++) {
temp[i] = nums[i];
}
// Left and right section boundaries
int mid = (left + right) >> 1;
// Make right pointer
int leftIdx = left;
int rightIdx = mid + 1;
// Number of pairs in reverse order
int count = 0;
// Merge range
for (int i = left; i <= right; i++) {
if (leftIdx == mid + 1) {
nums[i] = temp[rightIdx++];
} else if (rightIdx == right + 1) {
nums[i] = temp[leftIdx++];
} else if (temp[leftIdx] <= temp[rightIdx]) {
nums[i] = temp[leftIdx++];
} else {
// The current element of the left pointer is larger than the current element of the right pointer , Count the number of pairs in reverse order
count += mid - leftIdx + 1;
nums[i] = temp[rightIdx++];
}
}
return count;
}
边栏推荐
- 基于pytorch卷积人脸表情识别–毕业设计「建议收藏」
- Change password, confirm password verification antd
- Table list filter results remain unchanged
- Parent and child of treeselect
- Unity - "synthetic watermelon" small game notes
- Basic exercises of DQL in MySQL
- Blue Bridge Training (additional interview questions) day 7
- Auto.js enables Taobao to quickly submit orders
- Leetcode · daily question · 1331. array sequence number conversion · discretization
- MySQL practice -- master-slave replication
猜你喜欢

为什么说Crypto游戏正在改变游戏产业?

屈辱、抗争、逆转,三十年,中国该赢微软一次了

Using auto.js to realize fifaol3 brush teaching assistant

Protective bearish strategy
![[matlab] IIR filter](/img/60/8e666bff3d458cdd9367ca45112b92.png)
[matlab] IIR filter

Intrinsic value and time value of options

FFT wave simulation

How much do you know about JVM memory management

Jenkins -- continuous integration server

《如何打一场数据挖掘赛事》入门版
随机推荐
【ECMAScript6】Promise
Debezium系列之:2.0.0.Beta1的重大变化和新特性
Array, string de duplication
Leetcode 笔记 118. 杨辉三角
蓝桥集训(附加面试题)第七天
Shell基础概念和变量
Risk analysis of option trading
【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...
butterfly spreads
Map tiles: detailed explanation of vector tiles and grid tiles
Unity—“合成大西瓜”小游戏笔记
Call / put option price curve
The form select in antd is received before it is selected
我秃了!唯一索引、普通索引我该选谁?
Have a part of the game, after NFT is disabled in my world
How does the vditor renderer achieve server-side rendering (SSR)?
Night God simulator packet capturing wechat applet
Leetcode notes 566. Reshaping the matrix
Half wave rectification light LED
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088