Interview questions 36_ in the array of inverse pairs

Source: Internet
Author: User

Title Descriptionin the array of two digits, if the previous number is greater than the number that follows, then these two numbers form an inverse pair.    Enter an array to find the total number of reverse pairs in this array.
Thinking of solving problems
idea one: Violent search, a judgment of each number, the number after which there is no smaller than it, if there is a count plus one. Time O (n^2)
thinking Two: Using the merge sorting method, when merging, calculate the number of reverse order. See the code for the specific process. Time O (nlogn)
Implementation Code
Class Solution {Public:int inversepairs (vector<int> data) {if (Data.empty ()) return 0;        int len = Data.size ();                vector<int> copy (len,0);        int result=0;                result = MergeSort (data, copy,0,len-1);    return result; } int MergeSort (vector<int> &data, vector<int> &copy, int start, int end) {if (start = = E            nd) {Copy[start] = Data[end];        return 0;        } int mid = (End+start)/2;        int left = MergeSort (Data,copy,start, mid);        int right = MergeSort (data,copy, mid+1,end);                int count = merge (Data,copy,start,mid,end);    Return left + right +count; } int Merge (vector<int> &data, vector<int> &copy, int start, int mid, int end) {int Lbeg        in = start, LEnd = mid;        int rbegin = mid+1, rEnd = end;        int k=end;        int count = 0; while (lEnd >= lbegin &&Amp                REnd >= rbegin) {if (Data[lend] > Data[rend]) {count + = rend-rbegin+1;//calculated in reverse order            copy[k--] = data[lend--];         } else copy[k--] = data[rend--];                } while (Lbegin <= lEnd) copy[k--] = data[lend--];              while (Rbegin <= rEnd) copy[k--] = data[rend--];                for (int i=start; i<=end; i++) data[i] = Copy[i];    return count; }};


                                                                                                                

Interview questions 36_ in the array of inverse pairs

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.