Reverse Pairs
Given an arraynums, we call(i, j)animportant reverse pairifi < jandnums[i] > 2*nums[j].
You need to return the number of important reverse pairs in the given array.
Example1:
Input
: [1,3,2,3,1]
Output
: 2Example2:
Input
: [2,4,3,5,1]
Output
: 3Note:
The length of the given array will not exceed
50,000.
All the numbers in the input array are in the range of 32-bit integer.
分析
顺序loop数,所以当前数字来的时候,只能和它前面的数字比较它的2x+1
和前面count of smaller number,用map存数字和ordered index。本题直接排序再用二分查找。
BIT
update 和query反过来,update更新比它小的index,query取比它大的,然后正序loop。本题就是要找比J小的I,所以update每次更新的Index都是比当前小的
普通思路,这里Getcnt后面做法不同
因为Update高位集合了低位的和,所以 query(n) -query(2x+1)得到2x+1-n之间的所有数字。
分治
左右段mergesory后,左半端和右半段比较得count。同时左右半段sort merge
Last updated
Was this helpful?