For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.
return total of reverse pairs in A.

 
Example

Given A = [2, 4, 1, 3, 5] , (2, 1), (4, 1), (4, 3) are reverse pairs. return 3

分析:

如果用两个for loop,非常简单。但要降低复杂度,做法非常变态.简单来说,原理就是利用merge sort的方法,把左subarray 和右subarray 合并(假设它们都已排好序)。然后比较当前左subarray的第一个值是否比右边subarray第一个值大两倍,如果是,那么很明显左subarray所有的都比那个值大两倍。

 class Solution {
public int reversePairs(int[] nums) {
return mergeSort(nums, , nums.length - );
} public int mergeSort(int[] nums, int start, int end) {
if (start >= end) return ;
int total = ;
int mid = start + (end - start) / ;
total += mergeSort(nums, start, mid);
total += mergeSort(nums, mid + , end);
total += merge(nums, start, mid, end);
return total;
} public int merge(int[] nums, int start, int mid, int end) {
int[] temp = new int[end - start + ];
int p1 = start, p2 = mid + ;
int index = ,count = ; while (p1 <= mid && p2 <= end) {
if ((long) nums[p1] > * ((long) nums[p2])) {
count += mid - p1 + ;
p2++;
} else {
p1++;
}
}
p1 = start;
p2 = mid + ;
while (p1 <= mid && p2 <= end) {
if (nums[p1] < nums[p2]) {
temp[index++] = nums[p1++];
} else {
temp[index++] = nums[p2++];
}
} while(p1 <= mid) {
temp[index++] = nums[p1++];
}
while(p2 <= end) {
temp[index++] = nums[p2++];
}
System.arraycopy(temp, , nums, start, end - start + );
return count;
}
}

Reverse Pairs的更多相关文章

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

  2. [LeetCode] Reverse Pairs 翻转对

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

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

  4. LeetCode -Reverse Pairs

    my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=num ...

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

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

  7. LintCode 532: Reverse Pairs

    LintCode 35: Reverse Linked List 题目描述 翻转一个链表. 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1 ...

  8. 【leetcode】493. Reverse Pairs

    题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...

  9. leetcode 493 Reverse Pairs

    题意:给定一个数组nums,求若 i<j and nums[i] > 2*nums[j] 的逆序对. Note: 数组的长度不会超过50,000 不愧是hard模式的题目,虽然已经知道可以 ...

随机推荐

  1. MyEclipse 8.5配置Tomcat 7.0

    MyEclipse 8.5配置Tomcat 7.0 在窗口(Windows)->首选项(Prefrences)->MyEclipse->Servers->Tomcat 6.x下 ...

  2. 【CodeForces 618C】Constellation

    题 Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars n ...

  3. 细菌觅食算法-python实现

    BFOIndividual.py import numpy as np import ObjFunction class BFOIndividual: ''' individual of bateri ...

  4. 配置Junit测试程序

    第一步:加载所需要的包:右键-->Build Path-->Configure Build Path-->Libraries-->Add Library-->Junit ...

  5. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  6. jQuery的查找

    children([expr])概述 :取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合.可以通过可选的表达式来过滤所匹配的子元素.注意:parents()将查找所有祖辈元素,而child ...

  7. MyEclipse使用SVN进行项目版本控制

    一.搭建SVN服务器. 例如,使用VisualSVN Server,下载后安装. (1)在Repositories(版本库)上右击,新建Repository,选择Regular FSFS reposi ...

  8. 初学Hibernate之Query扩展

    1.hql参数化查询,不明确值类型的用setParameter方法:明确查询结果为一条记录的用uniqueResult方法查询 注意,参数化查询中方法setString 或 setParameter如 ...

  9. ModSecurity SQL注入攻击

    ModSecurity是 一个入侵探测与阻止的引擎,它主要是用于Web应用程序所以也可以叫做Web应用程序防火墙.它可以作为Apache Web服务器的一个模块或单独的应用程序来运行.ModSecur ...

  10. C#比较时间大小 1、比较时间大小的实验

    1.比较时间大小的实验    string st1="12:13"; string st2="14:14"; DateTime dt1=Convert.ToDa ...