The sword refers to the offer surface question 36-reverse order in the array

Source: Internet
Author: User

Topic:

Two digits in the array if the previous number is greater than the number that follows, the two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array. For example in the array {7,5,6,4} There are altogether 5 pairs of reverse pairs, respectively (7,6), (7, 5 ), (7,4), (6,4), (5,4)


Basic idea:

Solution One: O (n^2)

The simplest idea is to iterate over each element to compare it to the following element, if it is greater than count++, but the time complexity is O (n^2).


Solution Two: O (NLOGN)

Merge sort ideas:


For example, 7,5,4,6 can be divided into two segments 7,5 and 4, 62 sub-arrays

1. Find the reverse order in 7,5 because 7 is greater than 5 so there are 1 pairs

2. Find the reverse order in the 6,4, because 6 is greater than 4, so the reverse is added 1, 2

3.7,5 and 6, 4 are sorted, the result is 5, 7, and 4,6

4. Set two pointers to a maximum of two sub-arrays, P1 point 7,p2 to 6

5. Compare the values pointed to by P1 and P2, if it is greater than P2, because P2 points to the maximum value, so there are several elements in the second sub-array there are several pairs of reverse pairs (currently there are two elements, reverse to add 2,2+2=4), 7>6, after comparing the P1 point to the value into the auxiliary array, The auxiliary array now has a number 7 and then moves the P1 forward one point to 5

6. Again determine the value that P1 and P2 point to, p1 is less than P2, because P1 points to the largest value in the first subarray, so there is no number in the sub-array that can be reversed against the 6 that the current P2 points to, put the value of the P2 point into the auxiliary array, and move forward one to 4, where the auxiliary array is 6

7. Continue to Judge P1 (point 5) and P2 (point 4), 5>4, there is only one number in the second sub-array, reverse to add 1,4+1=5, 5 pairs, and then put 5 into the auxiliary array, the first sub-array is completed, only the second sub-array, currently only one 4, 4 also into the auxiliary array, The function ends. The secondary array is now 4,5,6,7. Reverse is 5.

    #include <iostream> using namespace std; int Inversepairscore (int* data,int* copy,int start,int end) {if (start = = end) {copy[              Start]=data[start];          return 0;          } int length = (end-start)/2;          int Left=inversepairscore (copy,data,start,start+length);                int Right=inversepairscore (copy,data,start+length+1,end);  int i=start+length;           I initialize the subscript int j=end to the last digit of the first half;          J initializes the subscript int indexcopy=end to the last digit in the second half of the paragraph;         int count=0;                  while (I>=start && j>= start+length+1) {if (Data[i] >data[j]) {                  copy[indexcopy--] =data[i--];              Count+=j-start-length;              } else {copy[indexcopy--]=data[j--];          }} for (; i>=start;--i) copy[indexcopy--]=data[i]; for (; J>=start+lenGTH+1;--J) Copy[indexcopy--]=data[j];      return left+right+count;          } int Inversepairs (int* data,int length) {if (data = = NULL | | length <0) return 0;          int* Copy=new Int[length];          for (int i =0;i<length;i++)//initialization of auxiliary arrays Copy[i]=data[i];          int Count=inversepairscore (DATA,COPY,0,LENGTH-1);          delete[] copy;      return count;          } void Main () {int a[4]={7,5,6,4};          int Result=inversepairs (a,4);     cout<<result<<endl;   }

the time complexity is O (Nlogn), but for an auxiliary array of length n, the spatial complexity is O (n).

The sword refers to the offer surface question 36-reverse order in the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.