forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 8
/
_493.java
47 lines (38 loc) · 1.17 KB
/
_493.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.fishercoder.solutions;
import java.util.Arrays;
/**
* 493. Reverse Pairs
*
* Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j].
You need to return the number of important reverse pairs in the given array.
Example1:
Input: [1,3,2,3,1]
Output: 2
Example2:
Input: [2,4,3,5,1]
Output: 3
Note:
The length of the given array will not exceed 50,000.
All the numbers in the input array are in the range of 32-bit integer.
*/
public class _493 {
/**credit: https://discuss.leetcode.com/topic/78933/very-short-and-clear-mergesort-bst-java-solutions*/
public int reversePairs(int[] nums) {
return mergeSort(nums, 0, nums.length - 1);
}
private int mergeSort(int[] nums, int s, int e) {
if (s >= e) {
return 0;
}
int mid = s + (e - s) / 2;
int cnt = mergeSort(nums, s, mid) + mergeSort(nums, mid + 1, e);
for (int i = s, j = mid + 1; i <= mid; i++) {
while (j <= e && nums[i] / 2.0 > nums[j]) {
j++;
}
cnt += j - (mid + 1);
}
Arrays.sort(nums, s, e + 1);
return cnt;
}
}