Question 36: Reverse order in an array

Source: Internet
Author: User

Topic:

In 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.

Ideas:

1. Sequential scan

Sequentially scan the entire array, each scan to a number, the number is compared with the following numbers, if greater than, then these two numbers constitute a reverse pair. (relatively simple, no code is posted here)

Complexity of Time: O (n^2)

2. Merging ideas

The array is continuously divided into two sub-arrays, and then the bottom-up statistics of the two sub-arrays of their respective reverse pairs and the combined reverse order.

Assuming that the two sub-arrays are A, B, the length is divided into Lena,lenb, the sub-array is ordered, then how to calculate the number of reverse pairs?

When PA=LENA-1;PB=LENB-1;A[PA]>A[PB], indicates that the last number of a is larger than the number of B, at this time the number is lenb,pa--; if A[PA]<B[PB], the left side of the number is not necessarily greater than the right, pb-- , in turn, until PA or PB is 0, so that two arrays are merged in reverse order, plus the left sub-array of reverse pairs and right sub-array of reverse pairs, is equal to the whole array of reverse order.

(The code cleverly uses a secondary array to sort, refer to the following code)

Complexity of Time: O (NLOGN)

Code:
#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) >>1;    int Left=inversepairscore (copy,data,start,start+length);    int Right=inversepairscore (copy,data,start+length+1,end);    int i=start+length;    int j=end;    int copyindex=end;    int count=0;            while (I>=start && j>=start+length+1) {if (Data[i]>data[j]) {copy[copyindex]=data[i];            copyindex--;            i--;        count+=j-(start+length);            } else{Copy[copyindex]=data[j];            copyindex--;        j--;    }} for (; i>=start;i--) copy[copyindex--]=data[i];    for (; j>=start+length+1;j--) copy[copyindex--]=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++) copy[i]=data[i];    int Count=inversepairscore (DATA,COPY,0,LENGTH-1);    delete[] copy; return count;}    int main () {int a[]={7,5,6,4};    int length=sizeof (A)/sizeof (a[0]);    cout << inversepairs (a,length) << Endl; return 0;}
Online test OJ:

http://www.nowcoder.com/books/coding-interviews/96bd6684e04a44eb80e6a68efc0ec6c5?rp=2

AC Code:

Class Solution {Public:int inversepairs (vector<int> data) {int length=data.size ();        if (length<=1) return 0;        int start=0;        int end=length-1;        int Result=inversepairscore (data,start,end);    return result; } int Inversepairscore (vector<int> &data,int start,int end) {if (Start>=end) return 0        ;        int mid=start+ ((end-start) >>1);        int Left=inversepairscore (DATA,START,MID);                int Right=inversepairscore (data,mid+1,end);        int I=start;        int j=mid+1;        int count=0;        vector<int> tmp;                while (I<=mid && j<=end) {if (Data[i]>data[j]) {count+=end-j+1;                Tmp.push_back (Data[i]);            i++;                } else{Tmp.push_back (Data[j]);            j + +;        }} while (I<=mid) Tmp.push_back (data[i++]); while (j<=end) Tmp.push_back (data[j++]);                for (int k=start;k<=end;k++) Data[k]=tmp[k-start];    return left+count+right; }};

 

Class Solution {Public:int inversepairs (vector<int> data) {int length=data.size ();        if (length<=0) return 0;        Vector<int> copy (Data.begin (), Data.end ());        int start=0;        int end=length-1;        int Count=inversepairscore (data,copy,start,end);    return count; } int Inversepairscore (vector<int> &data,vector<int> &copy,int start,int end) {if (start=            =end) {Copy[start]=data[start];        return 0;        } int length= (End-start) >>1;        int Left=inversepairscore (copy,data,start,start+length);                int Right=inversepairscore (copy,data,start+length+1,end);        int i=start+length;        int j=end;        int copyindex=end;        int count=0; while (I>=start && j>=start+length+1) {if (Data[i]>data[j]) {Copy[copyindex--]=da                ta[i--];            count+=j-(start+length);    } else            copy[copyindex--]=data[j--];        } for (; i>=start;i--) copy[copyindex--]=data[i];                for (; j>=start+length+1;j--) copy[copyindex--]=data[j];    return left+count+right; }};

(point of Offer) question 36: Reverse pairs in an 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.