题意:给定一个数组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. 基于size的优化

    ----------------------siwuxie095                                 基于 size 的优化         在 union( p , q ...

  2. Setting up an OpenGL development environment in ubuntu

    1.opening terminal window and entering the apt-get command for the packages: sudo apt-get install me ...

  3. php中不借助IDE快速定位行数或者方法定义的文件和位置

    借助了ReflectionMethod的一些特性,可以快速获得函数或者方法是在哪个文件的什么位置定义的,对于调试没有文档的程序来说很有帮助! ---------------- function fun ...

  4. Win10系统优化/设置脚本

    Win10系统优化/设置脚本 用了很长时间win10了,用的过程中,发现了一些问题,关于系统基本的优化,和个人的使用习惯设置等等,做成了一个脚本,可以一键设置win10的系统设置,结合DWS对Win1 ...

  5. Python3.7安装Django

    一.系统环境 操作系统:Win7 64位 Python版本:3.7 二.安装参考 Django的下载网址:https://www.djangoproject.com/download/ 当前最新版本: ...

  6. solr第一天 基础增删改查操作

    全文检索技术   Lucene&Solr               Part2 1 课程计划 1.索引库的维护 a) 添加文档 b) 删除文档 c) 修改文档 2.Lucene的查询 a)  ...

  7. 专题1-MMU-lesson3-MMU配置与使用

    1.段方式MMU 利用虚拟地址然后找到物理地址,通过物理地址访问到led,其过程如下: 一个段的大小是[19:0]总共有1M的地址空间. 从上面可知对应GPIO的段物理基地址是0x7f000000.那 ...

  8. JavaScript——Dom编程(1)

    DOM:Document Object Model(文本对象模型) D:文档 – html 文档 或 xml 文档O:对象 – document 对象的属性和方法M:模型 DOM 是针对xml(htm ...

  9. 进程间传递文件描述符fd

    众所周知,子进程会继承父进程已经打开的文件描述符fd,但是fork之后的是不会被继承的,这个时候是否无能无力了?答应是NO.Linux提供了一个系统调用sendmsg,借助它,可以实现进程间传递文件描 ...

  10. 第04章-面向切面的Spring

    1. 什么是面向切面编程 AOP是什么 切面帮助我们模块化横切关注点. 横切关注点可被描述为影响应用[多处的]功能.如安全,应用许多方法会涉及安全规则. 继承与委托是最常见的实现重用 通用功能 的面向 ...