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. SAP PP顾问面试题及资料

    SAP PP顾问面试试题及资料 1.主数据 组织架构 •SAP中主要的组织架构有哪些?哪些用于PP的组织架构?•成本控制范围.公司代码.估价范围.工厂.库存地点之间的关系 物料主数据 •不同的物料主数 ...

  2. zabbix2.2 - /tmp/FromDualMySQLagent.lock already exists

    最近升级了线上的zabbix server版本,升级成功后发现日志中一直报出history和history-uint表的主键冲突数据插入不成功的信息,根据主键冲突发生的itemid去库里查,如下 my ...

  3. html 響應式web設計

    RWD(響應式web設計)可以根據尺寸大小傳遞網頁,對於平板和移動設備是必須的. <html lang="en-US"> lang表示頁面的主要語言.http://ww ...

  4. 同步工具:CountDownLatch、CyclicBarrier和Semaphore

    1. CountDownLatch 1.1 功能及使用场景 一个同步工具,使得一个或多个线程等待一组线程执行完成后再执行. 使用场景:等待一些前置任务执行完成后,再执行特定的功能.比如,系统启动时,各 ...

  5. Fiddler4入门——手机抓包

    一.下载工具包 百度搜索”fiddler 下载“ ,安装最新版本 下载的软件安装包为“fiddler_4.6.20171.26113_setup.exe”格式,双击安装.安装成功,在“开始”-“所有程 ...

  6. Codeforces Round #428 (Div. 2)A,B,C

    A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. NOIP2013题解

    NOIP2013题解 Day1 转圈游戏 circle 快速幂模板题. #include<iostream> using namespace std; int n,m,k,x; int f ...

  8. [USACO 2018 December Contest]作业总结

    t1 Convention 题目大意 每一头牛都有一个来的时间,一共有\(n\)辆车,求出等待时间最长的那头牛等待的最小时间. 解法 第一眼看到这道题还以为是\(2018noip\)普及组的t3魔鬼题 ...

  9. 洛谷 P4097 [HEOI2013]Segment 解题报告

    P4097 [HEOI2013]Segment 题目描述 要求在平面直角坐标系下维护两个操作: 在平面上加入一条线段.记第 \(i\) 条被插入的线段的标号为 \(i\) 给定一个数 \(k\),询问 ...

  10. 51nod1237 最大公约数之和 V3

    题意:求 解: 最后一步转化是因为phi * I = Id,故Id * miu = phi 第二步是反演,中间省略了几步... 然后就这样A了......最终式子是个整除分块,后面用杜教筛求一下phi ...