题意:给定一个数组nums,求若 i<j and nums[i] > 2*nums[j] 的逆序对。

Note: 数组的长度不会超过50,000

不愧是hard模式的题目,虽然已经知道可以用归并排序来做,但是写出来的答案总有问题,真的是暴风哭泣 :(

一直在找bug,最后发现是我写的merge函数有问题,分析一下:

这是我一开始写的错误的merge函数,我是在 aux[i] > aux[j] 里面加入了若也满足 aux[i] > 2*aux[j] 来计算res

 int merge(vector<int>& arr, vector<int>& aux, int l, int mid, int r){
for(int i=l;i<=r;i++){
aux[i] = arr[i];
}
int res = ; //统计逆序对数量
int i = l, j = mid+;
for(int k=l; k<=r;k++){
if(i>mid){
arr[k] = aux[j];
j++;
}
else if(j>r){
arr[k] = aux[i];
i++;
}
else if(aux[i]<aux[j]){
arr[k] = aux[i];
i++;
}
else{
arr[k] = aux[j];if(aux[i] > *aux[j])
res+=(mid-i+);
          j++;
}
}
return res;
}

我一直不知道自己的解法为什么不对,然后参考了别人的解法,改了一下merge函数。

我觉得错误的地方是把 if(aux[i] > 2*aux[j]) 这个语句放在了 aux[i] > aux[j] 里 ,只有当同时满足这两个条件时res才会增加,但是若不满足2倍的大于关系时,j++ , 这样就会丢掉一些解。所以要把它拿出来另外判断!

正确解法如下:

class Solution {
public:
int mergeSort(vector<int>& nums, vector<int>& aux, int l, int r){
if(l >= r) return ;
int mid = (l+r)/; //(l+r)>>1
int left = mergeSort(nums, aux, l, mid);
int right = mergeSort(nums, aux, mid+,r);
int pair = left + right + merge(nums, aux, l, mid, r);
return pair;
} int merge(vector<int>& arr, vector<int>& aux, int l, int mid, int r){
for(int i=l;i<=r;i++){
aux[i] = arr[i];
} int res = ; //统计逆序对数量 int i = l, j = mid+;
for(int k=l; k<=r;k++){
if(i>mid){
arr[k] = aux[j];
j++;
}
else if(j>r){
arr[k] = aux[i];
i++;
}
else if(aux[i]<aux[j]){
arr[k] = aux[i];
i++;
}
else{
arr[k] = aux[j];
j++;
}
} /* for(int s=l,t=mid+1;s<=mid;s++){
while(t<=r && aux[s] > 2LL*aux[t]){
res += mid-s+1;
t++;
}
}
*/
for(int s=l,t=mid;s<=mid;s++){
while(t+<=r && aux[s] > 2LL*aux[t+]) t++;
res += t-mid; // t-(mid+1)+1 = t-mid
} return res;
} int reversePairs(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
vector<int> aux(nums);
return mergeSort(nums, aux, , n-);
}
};

hard模式 真是不容易 == 刷题真难

leetcode 493 Reverse Pairs的更多相关文章

  1. [LeetCode] 493. Reverse Pairs 翻转对

    Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...

  2. 第二周 Leetcode 493. Reverse Pairs(HARD)

    leetcode 493跟经典的逆序对问题没有什么区别, 首先考虑对数组前半部和后半部求逆序对数,若能保证两段数组都有序,则显然可以在线性时间内求出对数. 所以我们采用归并排序的方法,一方面让数组有序 ...

  3. 493. Reverse Pairs(BST, BIT, MergeSort)

    Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...

  4. 【leetcode】493. Reverse Pairs

    题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...

  5. 493. Reverse Pairs

    // see more at https://www.youtube.com/watch?v=j68OXAMlTM4 // https://leetcode.com/problems/reverse- ...

  6. 493 Reverse Pairs 翻转对

    给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...

  7. LeetCode -Reverse Pairs

    my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=num ...

  8. [LeetCode] Reverse Pairs 翻转对

    Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...

  9. [LintCode] Reverse Pairs 翻转对

    For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...

随机推荐

  1. EKF model&realization

    REF: https://pythonrobotics.readthedocs.io/en/latest/modules/localization.html#unscented-kalman-filt ...

  2. SaeStorage使用示例

    新浪SAE官方地址:http://apidoc.sinaapp.com/sae/SaeStorage.html SaeStorage的代码详细:http://apidoc.sinaapp.com/__ ...

  3. 447. Number of Boomerangs 回力镖数组的数量

    [抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

  4. ZendStudio 代码调试

    F5.单步调试进入函数内部(单步进入)F6.单步调试不进入函数内部(跳过)F7.由函数内部返回到调用处(跳出) F8.一直执行到下一个断点Ctrl+F2:结束调试

  5. linux 首次登陆与线上求助

    开始下达指令概念 上述指令详细说明如下:1. 一行指令中第一个输入的部分绝对是『指令(command)』或『可执行文件案(例如批次脚本,script)』2. command 为指令的名称,例如变换工作 ...

  6. gettimeofday()函数的使用方法

    1.简介: 在C语言中可以使用函数gettimeofday()函数来得到精确时间.它的精度可以达到微妙,是C标准库的函数. 2.函数原型: #include<sys/time.h> int ...

  7. Oracle 写存储过程的一个模板还有一些基本的知识点

    我很少用Oracle,也算新手,不过其实入手没有那么难,下面只是一个基本知识,高手绕道,其实数据库基本是相同的,这里提供都是基本知识点 有一个Oracle溢出的问题,容易让新手怀疑到无所怀疑,其实就是 ...

  8. Android getDimension,getDimensionPixelOffset,getDimensionPixelSize

    1.例如在onMeasure(int , int)方法中可能要获取自定义属性的值.如: TypedArray a = context.obtainStyledAttributes(attrs, R.s ...

  9. Server Sql 多表查询、子查询和分页

    一.多表查询:根据特定的连接条件从不同的表中获取所需的数据 多表查询语法: SELECT table1.column, table2.column FROM table1, table2 WHERE ...

  10. this指针------新标准c++程序设计

    背景:   c++是在c语言的基础上发展而来的,第一个c++的编译器实际上是将c++程序翻译成c语言程序,然后再用c语言编译器进行编译.c语言没有类的概念,只有结构,函数都是全局函数,没有成员函数.翻 ...