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. Java线程池应用

    Executors工具类用于创建Java线程池和定时器. newFixedThreadPool:创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThread ...

  2. myeclipse自动import

    不管包什么的 直接把代码全写出来 再按 ctrl + shift +o 这是自动导包的 前提是你写的代码是正确的

  3. javascript与服务器2

    一, 通过IE建立XMLHTTP请求 这种请求方式源自与微软创建了XMLHTTP请求对象,只要用户的机器上安装了MSXML就可以发送XMLHTTP请求来创建客户端和服务器端的通信, XMLHTTP请求 ...

  4. G-nav-02

    /*header: Navigation public style*/header:before, header:after ,.navigation:before, .navigation:afte ...

  5. ansible 的组件inventory

    P44 Ansible 的默认的inventory的是一个静态的ini格式的文件/etc/ansible/hosts. 我们还可以通过ansible_hosts环境变脸指定或者运行ansible和an ...

  6. Spring MVC框架

    这个Spring Web MVC 框架提供了模型视图控制器的架构,这种结构能够被用来开发灵活的和松耦合的Web应用程序. 这种MVC模式能够将应用程序分离成不同的层面,(输入逻辑,业务逻辑,UI逻辑) ...

  7. Html设置图标icon

    html head添加: <link rel="icon" href="/favicon.ico" type="image/x-icon&quo ...

  8. Asp.Net MVC 中实现跨域访问

    在ASP.Net webapi中可以使用  Microsoft.AspNet.WebApi.Cors  来实现: public static class WebApiConfig { public s ...

  9. 让js的forin循环禁止forin到某个属性的话要怎么做

    //知识点1:for In循环是可以枚举到继承的属性的://知识点2:使用defineProperty让属性无法通过forIn枚举到://知识点3:用definedProperty重新定义一个属性药把 ...

  10. 【Gym 100971A】Treasure Island

    题意 题目链接给你一个地图,'#'代表水,'.'代表陆地,'?'代表擦去的地图,可能是'#'也可能是'.'.地图中本该只有一块相连的陆地,若只有一种方案则输出确定的地图.若有多种方案,则输出‘Ambi ...