Wins the offer-36: reverse in the array

Reference:. 1,  https://www.geeksforgeeks.org/merge-sort/

           2, "to prove safety Offer: famous enterprises interviewer succinctly typical programming problem."

Title Description

Two numbers in the array, if a number is greater than the front behind the figures, the two numbers form a reverse pair. Enter an array, this array P. finds the total number of reverse pair P and outputs the result modulo 1000000007. I.e., the output P% 1000000007

Enter a description:

Title ensure the same number of the input array is not

data range:

  • 50% of the data, size <= 10 ^ 4
  • 75% of the data, size <= 10 ^ 5
  • 100% of the data, size <= 2 * 10 ^ 5
Example 1 
Input: 1,2,3,4,5,6,7,0 
Output: 7

 

Outline of Solution 1 (violence, time complexity: O (n ^ 2))

This method is the most simple and direct, sequential scanning of the entire array, each swept to a number, one by comparing the number of all behind it, if it constitutes reverse order, the count +1. Code is nothing more difficult, a little.

 

Outline of Solution 2 (divide and conquer method, time complexity: O (nlogn), the spatial complexity: O (n))

 1     public int InversePairs(int[] array) {
 2         if (array.length < 2) {
 3             return 0;
 4         }
 5         return divideAndConquer(array, 0, array.length);
 6     }
 7 
 8     public int divideAndConquer(int[] array, int l, int r) {
 9         if (l >= r - 1) return 0;
10 
11         int mid = (l + r) / 2;
12         int count = 0;
13         int countL = divideAndConquer(array, l, mid);
14         int countR = divideAndConquer(array, mid, r);
15         int[] left = Arrays.copyOfRange(array, l, mid);
16         int[] right = Arrays.copyOfRange(array, mid, r);
17         int i = left.length - 1;
18         int j = right.length - 1;
19         for (int k = r - 1; k >= l; k--) {
20             // two cases: left is empty or right is empty
21             if (i < 0) {
22                 array[k] = right[j];
23                 j--;
24                 continue;
25             } else if (j < 0) {
26                 array[k] = left[i];
27                 i--;
28                 continue;
29             }
30             if (left[i] > right[j]) {
31                 count += j + 1;
32                 if (count > 1000000007) {
33                     count %= 1000000007;
34                 }
35                 array[k] = left[i];
36                 i--;
37             } else {
38                 array[k] = right[j];
39                 j--;
40             }
41         }
42         return (count + countL + countR) % 1000000007;
43     }

Solution of this problem is based on merge sort (Merge Sort) on the modification obtained. Merge Sort using ideological divide and conquer method. Method partition (divide and conquer) is a complex issue, into two or more similar or identical sub-problems, then the sub-problems into smaller sub-problems, has been assigned simple enough to solve sub-problems, and finally sub-problem solution and then combined into a solution to the original problem. Merge sort time complexity of O (nlogn), the spatial complexity + temporary array recursive data pushed onto the stack when the space occupied = n + logn, i.e., O (n).

 

An array {7, 5, 6, 4} example for analysis, the problem solving process shown in Figure 5.2. First, the array is split into sub-arrays to a length of 1, and while the number of combined reverse side of the statistics. In the first sub-array length 7 {1}, {5}, there is a pair of reverse order (7, 5). In the second sub-array length 1 {6}, {4}, but also a pair of reverse order (6, 4). After the completion of these two statistics on the reverse of the need to sort them respectively, as shown in 5.2 (c), so as not to repeat the statistics in subsequent statistical process.

 

Statistical then need to reverse between two lengths of the sub-array 2, and merge two sub-arrays, shown in Figure 5.3. Two sub-arrays are determined size from the end of the number, in FIG. 5.2 (a) pointer P1, P2. If P1 is greater than P2 , to the reverse configuration, the number of reverse and a second sub-array is equal to the number of the remaining figures, i.e., P2, and the number of all the numbers before. If P1 is less than or equal to P2 , does not constitute a reverse order. When each comparison, we regard the higher number copied from the back to a secondary array to ensure that the auxiliary array of numbers is ascending order. After the large digital copying to the secondary array, corresponding to the forward movement of a pointer, then the next round of comparison.

And so on, use this method to obtain reverse order of the number of other array. When the solution of this problem, there is little need to pay attention, in addition to the total output (count + countL + countR) modulus of 1,000,000,007, but also in the calculation process will count + = j + 1 for modulo 1000000007, so as not to count beyond Integer.MAX_VALUE.

Guess you like

Origin www.cnblogs.com/xiaoyebula/p/12045554.html