LeetCode -Reverse Pairs

my solution:
class Solution {
public:
int reversePairs(vector<int>& nums) {
int length=nums.size();
int count=;
for (int i=;i<length;i++)
{
for(int j=i+;j<length;j++)
{
if(nums[i]>2*nums[j])
count++;
}
}
return count;
}
};
wrong answer :

because 2147483647*2 is -2 (int) , only 33bit can represent
| int | 4 byte | 32 bit |
2147483647--2147483647 signed 0-4294967295 unsigned |
| long | 4 byte | 32 bit | (在 32 位机器上与int 相同) |
| double | 8 byte | 64 bit | 1.79769e+308 ~ 2.22507e-308 |
| float | 4 byte | 32 bit | 3.40282e+038 ~ 1.17549e-038 |
| long double | 12 byte | 96 bit | 1.18973e+4932 ~ 3.3621e-4932 |
then i change the type to double:
class Solution {
public:
int reversePairs(vector<int>& nums) {
int length=nums.size();
int count=;
double a;
for (int i=;i<length;i++)
{
for(int j=i+;j<length;j++)
{
a=(double)*nums[j];
if(nums[i]>a)
count++;
}
}
return count;
}
};
wrong:

because the if the maxmise length is 50000, the process will lead to time exceed .
对于这类问题,一种很好的解法就是拆分数组来解决子问题,通过求解小问题来求解大问题。
有两类拆分方法:
- 顺序重现(sequential recurrence relation):T(i, j) = T(i, j - 1) + C
- C就是处理最后一个数字的子问题,找 T(i, j - 1)中的reverse pairs, T(i, j-1) 有序,只需要找大于2*nums[j]的数。 二分查找已经不满足条件了,只能使用Binary indexed tree 来实现。BIT的存储方式不是数组对应一一存入,而是有的对应存入,有的存若干个数字之和,其设计的初衷是在O(lgn)时间复杂度内完成求和运算。
- 分割重现(Partition Recurrence relation):T(i, j) = T(i, m) + T(m+1, j) + C
- C为合并两个部分的子问题。MergeSort的思想就是把数组对半拆分为子数组,柴刀最小的数组后开始排序,然后一层一层返回,得到有序的数组。时间复杂度O(nlogn)
class Solution {
public:
int reversePairs(vector<int>& nums) {
return mergeSort(nums, , nums.size() - );
}
int mergeSort(vector<int>& nums, int left, int right) {
if (left >= right) return ;
int mid = left + (right - left) / ;
int res = mergeSort(nums, left, mid) + mergeSort(nums, mid + , right);
for (int i = left, j = mid + ; i <= mid; ++i) {
while (j <= right && nums[i] / 2.0 > nums[j]) ++j;
res += j - (mid + );
}
sort(nums.begin() + left, nums.begin() + right + );
return res;
}
};
归并排序 MergeSort 和BIT可以解决,BST和 binary search不行
https://discuss.leetcode.com/topic/79227/general-principles-behind-problems-similar-to-reverse-pairsBST (binary search tree)
BIT (binary indexed tree)
LeetCode -Reverse Pairs的更多相关文章
- [LeetCode] Reverse Pairs 翻转对
Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...
- [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] ...
- [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 ...
- [Swift]LeetCode493. 翻转对 | Reverse Pairs
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...
- 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 ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- 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] ...
随机推荐
- Windows server 2008 r2下载地址和激活破解方法
Windows 7发布了服务器版本——Windows Server 2008 R2.同2008年1月发布的Windows Server 2008相比,Windows Server 2008 R2继续提 ...
- Java循环中try...finally...遇到continue
一段很简单的代码,先自己在大脑中给出结果: for (int i = 0; i < 5; i++) { System.out.println("enter: i=" + i) ...
- 洛谷P3588 [POI2015]PUS
题面 sol:说了是线段树优化建图的模板... 就是把一整个区间的点连到一个点上,然后用那个点来连需要连一整个区间的点就可以了,就把边的条数优化成n*log(n)了 #include <queu ...
- mybatis 缓存(cache)的使用
许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的.类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsessio ...
- 今天一天课,随便写点吧/xk
知道的越多,不知道的也就越多. 最近想学很多很多东西,但是发现没有一个计划,也没有那么多时间精力,都是想到哪学到哪,有的就是学了一半就放下了,又去学新的,感觉需要规划一下学习路线,时间什么的 推荐一个 ...
- 自学Linux Shell14.3-创建临时文件
点击返回 自学Linux命令行与Shell脚本之路 14.3-创建临时文件mktemp Linux系统保留了一个特殊的目录位置,以供临时文件使用.Linux使用/tmp目录处理不需要永久保存的文件. ...
- 【BZOJ1818】[CQOI2010]内部白点(树状数组,扫描线)
[BZOJ1818][CQOI2010]内部白点(树状数组,扫描线) 题面 BZOJ 题解 不难发现\(-1\)就是在搞笑的. 那么对于每一行,我们显然可以处理出来最左和最右的点,那么等价于我们在横着 ...
- 面试 -- requestLayout、invalidate与postInvalidate区别
requestLayout: 从方法名字可以知道,“请求布局”,那就是说,如果调用了这个方法,那么对于一个子View来说,应该会重新进行布局流程.但是,真实情况略有不同,如果子View调用了这个方法, ...
- Centos6.5使用yum安装mysql
0. 说明 先要查看yum源是否有你想要的mysql版本 yum list | grep mysql 如果没有则先要更新yum源 yum -y update 更新后即可进行下一步操作. 1. yum安 ...
- A1101. Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...