原题链接在这里:https://leetcode.com/problems/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.
题解:
It is obvious that it takes O(n^2) to solve it by brute force. The question is how to optimize it.
For question like it, try to cut it into small subproblems and use divide and conquer.
Usually it could be cut by 2 ways: T(i, j) = T(i, j-1) + nums[j]. Or T(i, j) = T(i, mid) + T(mid+1, j).
To optimize it, use the 2nd way here.
The reverse pairs could happen in 3 parts, T(i, mid), T(mid+1, j) and T(i, j).
Let divide function return the count of subproblem, and also sort the subarray.
Then when we get the count of subproblem, nums[l ~ mid] is alrady sorted, so is nums[mid+1, r].
Use two pointers i and j. Move j while nums[i]/2.0 > nums[j]. Here to avoid overflow, use /2.0 but not *2 on the other side. Also must use 2.0, but not 2. e.g. 3/2.0 > 1, but 3/2==1.
j - (mid+1) pairs found. Then move i. now nums[i] is bigger, j only needs to move forwad, there is no need to reset it to mid+1.
Accumlate the res and finally sort nums[i ~ j].
Time Complexity: O(n(logn)^2). T(n) = 2*T(n/2) + O(nlogn). Mater theorem, T(n) = O(n(logn)^2). Each level, it takes O(nlogn) for sorting. There are logn levels.
Space: O(logn). stack space.
AC Java:
1 class Solution { 2 public int reversePairs(int[] nums) { 3 if(nums == null || nums.length < 2){ 4 return 0; 5 } 6 7 return mergeSort(nums, 0, nums.length-1); 8 } 9 10 private int mergeSort(int [] nums, int l, int r){ 11 if(l >= r){ 12 return 0; 13 } 14 15 int mid = l + (r-l)/2; 16 int count = mergeSort(nums, l, mid) + mergeSort(nums, mid+1, r); 17 int i = l; 18 int j = mid+1; 19 while(i<=mid){ 20 while(j<=r && nums[i]/2.0 > nums[j]){ 21 j++; 22 } 23 24 count += j-(mid+1); 25 i++; 26 } 27 28 Arrays.sort(nums, l, r+1); 29 return count; 30 } 31 }
Since left and right parts are alrady sorted. Could use O(n) time to sort. This reduces time complexity to O(n) on each level.
Time Complexity: O(nlogn).
Space: O(logn).
AC Java:
1 class Solution { 2 int [] copy; 3 public int reversePairs(int[] nums) { 4 if(nums == null || nums.length < 2){ 5 return 0; 6 } 7 8 copy = new int[nums.length]; 9 return mergeSort(nums, 0, nums.length-1); 10 } 11 12 private int mergeSort(int [] nums, int l, int r){ 13 if(l >= r){ 14 return 0; 15 } 16 17 int mid = l + (r-l)/2; 18 int count = mergeSort(nums, l, mid) + mergeSort(nums, mid+1, r); 19 int i = l; 20 int j = mid+1; 21 while(i<=mid){ 22 while(j<=r && nums[i]/2.0 > nums[j]){ 23 j++; 24 } 25 26 count += j-(mid+1); 27 i++; 28 } 29 30 merge(nums, l, mid, r); 31 return count; 32 } 33 34 private void merge(int [] nums, int l, int mid, int r){ 35 for(int cur = l; cur<=r; cur++){ 36 copy[cur] = nums[cur]; 37 } 38 39 int i = l; 40 int j = mid+1; 41 int po = i; 42 while(i<=mid || j<=r){ 43 if(i>mid || (j<=r && copy[i]>copy[j])){ 44 nums[po++] = copy[j++]; 45 }else{ 46 nums[po++] = copy[i++]; 47 } 48 } 49 } 50 }