题意:给定一个数组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. mysql 纸 mysql_fetch_array OR mysql_fetch_assoc OR mysql_fetch_row

    <?php $con = mysql_connect("localhost", "root", "123456");if (!$con ...

  2. <c:out>标签中有一个escapeXml属性 如果为escapeXml="false",则将其中的html、xml解析出来。

    <td><c:out value="${s.name}" escapeXml="false"></c:out></td ...

  3. 243. Shortest Word Distance 最短的单词index之差

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

  4. Win10 linux子系统Ubuntu下显示图形界面

    转载 https://jingyan.baidu.com/article/ed2a5d1f98577809f6be17a3.html 打开终端界面,在这个窗口测试一下ls命令,无误. # 更新 sud ...

  5. Luogu 3665 [USACO17OPEN]Switch Grass 切换牧草

    BZOJ 4777 被权限了. 这道题的做法看上去不难,但是感觉自己yy不出来. 首先是两个结论: 1.答案一定是连接着两个异色点的一条边. 2.答案一定在最小生成树上. 感觉看到了之后都比较显然,自 ...

  6. WordCountPro

    github项目地址:https://github.com/Hoyifei/SQ-T-Homework-WordCount-Advanced PSP表格   PSP2.1 PSP阶段 预估耗时 (分钟 ...

  7. [GO]给导入包起别名

    package main import io "fmt" //引用fmt这个包时,名字重命名为io import _ "os" //引用os这个包,但是不调用, ...

  8. Django实战之古风博客

    感谢 感谢杨青 大大的古风模板,设计的很棒,给个赞. 如有侵权,请联系我 运行环境 python3.6 Django==1.11.4 django-ckeditor==5.4.0 django-js- ...

  9. MongoDB整理笔记のCRUD

    添加 下面我们来建立一个test 的集合并写入一些数据.建立两个对象j 和t , 并保存到集合中去.在例子里 “>” 来表示是 shell 输入提示符    > j = { name : ...

  10. winform panel显示子窗体

    private void ZiChuangTi() {//确认当前为子窗体 this.IsMdiContainer = true; //建立个子窗体的对象 Son mySon = new Son(); ...