leetcode 493 Reverse Pairs

题意:给定一个数组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的更多相关文章
- [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] ...
- 第二周 Leetcode 493. Reverse Pairs(HARD)
leetcode 493跟经典的逆序对问题没有什么区别, 首先考虑对数组前半部和后半部求逆序对数,若能保证两段数组都有序,则显然可以在线性时间内求出对数. 所以我们采用归并排序的方法,一方面让数组有序 ...
- 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] ...
- 【leetcode】493. Reverse Pairs
题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...
- 493. Reverse Pairs
// see more at https://www.youtube.com/watch?v=j68OXAMlTM4 // https://leetcode.com/problems/reverse- ...
- 493 Reverse Pairs 翻转对
给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: ...
- LeetCode -Reverse Pairs
my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=num ...
- [LeetCode] Reverse Pairs 翻转对
Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...
- [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 ...
随机推荐
- css二阴影+第三维度z-index
<span style="text-shadow: 3px 3px 1px #888;">shadowed</span><span style=&qu ...
- SpringAOP02 自定义注解
1 自定义注解 1.1 创建自定义注解 从java5开始就可以利用 @interface 来定义自定义注解 技巧01:注解不能直接干扰程序代码的运行(即:注解的增加和删除操作后,代码都可以正常运行) ...
- 14-stringstream
C++中stringstream的使用方法和样例 原创 2016年11月06日 15:46:49 标签: string / C++ 7427 之前在leetcode中进行string和int的转化时使 ...
- 我使用的网址--Hadoop
1.Hadoop 官网下载:http://hadoop.apache.org/releases.html 各版本网址:http://mirror.bit.edu.cn/apache/hadoop/co ...
- sam格式详细说明
原文链接 https://www.jianshu.com/p/386f520e5de1 The SAM Format Specification(sam格式说明) 1 The SAM Format S ...
- HTML中meta标签的作用与使用
META标签用来描述一个HTML网页文档的属性 META标签可分为两大部分:HTTP-EQUIV和NAME变量. HTTP实例 HTML代码实例中有一项内容是 <meta http-equiv= ...
- rest-framework-----视图
一:基本视图 写一个出版社的增删改查的resful接口 路由: url(r'^publish/$', views.PublishView.as_view()), url(r'^publish/(?P& ...
- App测试从入门到精通之UI测试
UI(user interface用户界面)的简称.UI测试也是APP测试中需要考虑的一个层面.用户至上,这个太重要了.一个好的App在界面的UI层设计上应该要满足简洁.美观.大气(这个是自己感觉的哈 ...
- [转]windows7远程桌面连接失败:发生身份验证错误。要求的函数不受支持
转至:https://jingyan.baidu.com/article/d169e18604ca86436611d821.html 系统升级后出现远程连接报错,“发生身份验证错误.要求的函数不受支持 ...
- netty使用以及聊天小程序
<从零开始搭建游戏服务器>Netty导入创建Socket服务器 Netty入门教程 Netty 聊天小程序