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的更多相关文章

  1. [LeetCode] Reverse Pairs 翻转对

    Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...

  2. [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] ...

  3. [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 ...

  4. [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] ...

  5. 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 ...

  6. 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 ...

  7. 2016.5.16——leetcode:Reverse Bits(超详细讲解)

    leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...

  8. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  9. 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] ...

随机推荐

  1. python学习笔记九——序列

    4.4 序列 序列是具有索引和切片能力的集合.元组.列表和字符串具有通过索引访问某个具体的值,或通过切片返回一段切片的能力,因此元组.列表和字符串都属于序列.序列索引功能演示: tuple=(&quo ...

  2. ERP开源框架 + 二次开发平台 介绍

    经历了多年软件开发,深受网络大侠们的资源共享才得以有所成绩, 本人主要是做企业ERP软件,一直有个感受,开发具体某个功能不难,但随着需求的增加,管理庞大的代码却成了最大的问题 而为企业管理所做的开发, ...

  3. rabbitmq安装与使用总结

    一.随着公司业务量的增加,原本部署在Windows服务器的RabbitMQ集群(3.6.1)总是出现莫名其妙的问题,经查询官方Issue,确认是RabbitMQ 3.6.1 版本的bug.查看从3.6 ...

  4. selenium之截图

    selenium支持对当前页面保存截图,使用方法: driver.get_screenshot_as_file(file_path) 代码举例: ...... def get_screenshot(d ...

  5. CPU性能过剩提升乏力影响未来行业发展吗?

    导读 虽然CPU仍然在不断发展,但是它的性能已经不再仅仅受限于单个处理器类型或制造工艺上了.和过去相比,CPU性能提升的步伐明显放缓了,接下来怎么办,成为横亘在整个行业面前的大问题. 虽然CPU仍然在 ...

  6. BZOJ4001[TJOI2015]概率论——卡特兰数

    题目描述 输入 输入一个正整数N,代表有根树的结点数 输出 输出这棵树期望的叶子节点数.要求误差小于1e-9 样例输入 1 样例输出 1.000000000 提示 1<=N<=10^9 设 ...

  7. BZOJ3307雨天的尾巴——线段树合并

    题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入 第一行数字N,M接下来N ...

  8. Picture POJ - 1177(扫描线求面积并)

    题意:求矩形并的面积.. 解析: 扫描线第一道题....自下而上扫描的... 如果不懂什么是扫描线戳我 #include <iostream> #include <cstdio> ...

  9. 18 Zabbix 新增map中的icon图标

    点击返回:自学Zabbix之路 18 Zabbix 新增map中的icon图标 zabbix系统默认会带有许多的icon图标,但对于特殊需求人群可能无法满足,那就需要新增icon图标. 通过Admin ...

  10. suoi07 区间平均++ (二分答案+前缀和)

    https://www.vijos.org/d/SUOI/p/59dc5af7d3d8a1361ae62b97 二分一个答案,然后做一做前缀和,用满足区间大小的最小值减一减,判断答案合不合法 然而还要 ...