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] ...
随机推荐
- mysql常用增删改查命令(纯纪录.orm用得基本功都没了。)
更新表数据: update table_name set xxx=xxx where condition; 增加字段: alter table table_name add field type ot ...
- js數字
js數字只有一種類型:不是類型語言. js的數字可以使用科學計數法或者不使用科學計數法: js都是64位的, 如果是整數,(不使用科學計數法或者是小數點)最大15位的: 如果是浮點數,最大17位的,浮 ...
- BZOJ1127 POI2008KUP(悬线法)
首先显然地,如果某个格子的权值超过2k,其一定不在答案之中:如果在[k,2k]中,其自身就可以作为答案.那么现在我们只需要考虑所选权值都小于k的情况. 可以发现一个结论:若存在一个权值都小于k的矩阵其 ...
- Mvc 前台 匿名对象
View无法使用 dynamic 报错: object 未包含....的属性 这里需要区别一下:如果dynamic只是简单模型,那么还是可以使用的.例如 dynamic v = new Expando ...
- JSP 和 Servlet 的工作原理和生命周期
JSP的英文名叫Java Server Pages,翻译为中文是Java服务器页面的意思,其底层就是一个简化的Servlet设计,是由sum公司主导参与建立的一种动态网页技术标准.Servlet 就是 ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- 洛谷P1776 宝物筛选_NOI导刊2010提高(02)(多重背包,单调队列)
为了学习单调队列优化DP奔向了此题... 基础的多重背包就不展开了.设\(f_{i,j}\)为选前\(i\)个物品,重量不超过\(j\)的最大价值,\(w\)为重量,\(v\)为价值(蒟蒻有强迫症,特 ...
- luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)
带修改区间K大值 这题有很多做法,我的做法是树状数组套权值线段树,修改查询的时候都是按着树状数组的规则找出那log(n)个线段树根,然后一起往下做 时空都是$O(nlog^2n)$的(如果离散化了的话 ...
- NinePatch图片
绍 参考 :http://blog.sina.com.cn/s/blog_5033827f0100r4dm.html NinePatch图片以*.9.png结尾,和普通图片的区别是四周多了一个边框(如 ...
- P2569 股票交易
题目大意: 你初始时有∞ 元钱,并且每天持有的股票不超过 Maxp . 有 T 天,你知道每一天的买入价格( AP[i] ),卖出价格( Bp[i] ), 买入数量限制( AS[i] ),卖出数量限制 ...