The ultimate solution for algorithms for integer array classes

Source: Internet
Author: User

The first thing to think about is that there is an array a = [1,2,3,4,5,4,3,2,1,2,3,4,5,4,3,2,1], and there are some elements in the array that have duplicate data, and now you are asked to give the number of elements in the right (left) side that are equal to each element in the array (excluding itself) , how many elements are greater than it, how many elements are twice times less than (greater than) it, 3 times times, squared .... Even more generally, how many elements (A[i],a[j]) (i<j<a.length) satisfy fun (A[i],a[j])?

One of the stupidest ways:

int res = 0;for (int i=0;i<a.length;i++) {for    (int. j=i+1;j<a.length;j++) {        if (fun (a[i],a[j]) = = true) Res + = 1;}    } return res;

The time complexity of this method is O (N*N/2), which is obviously difficult to satisfy.

In the field of algorithm, in order to reduce the complexity of the algorithm, it is necessary to improve the space complexity, that is, the so-called space exchange time. What if we want to change the space for time?

We traverse the array from the back forward, and each time we traverse an element, we put it into a warehouse, and then we ask the element to go directly to the warehouse when it has several qualifying values in the warehouse. as long as the find and add in the warehouse, delete fast enough (in O (C) time to complete), our time complexity will be reduced to O (c*n), which is obviously acceptable.

So is there a warehouse (data structure) that allows you to find, add, and delete elements in constant time? The answer is yes, it is the dictionary tree.

We know that each int has a 32-bit composition. And each one can only be 0 or 1. This allows you to describe all int as long as you create a two fork tree with a depth of 32. For each int insert, delete, find the time is 32, the visible dictionary tree can meet our needs.

Spread out the problem, the dictionary tree is suitable for creating a "warehouse" in which the aggregate can be described by [a finite dimension], and the time complexity of adding, modifying, and finding a single element is [dimension].


For example, the word frequency statistics of English articles, recording the frequencies of each occurrence of words, can be fully implemented using a dictionary tree. A dimension describing all words is the content of a word, a string of English characters whose length may vary, but is limited in general, i.e. [1,100]. English characters are also limited, and they can be distinguished by ASIIC code.

A key factor in using a good dictionary tree is the ability to find a finite set of dimensions to describe the entire set.

Then put a piece of code on it:

/*** * * * Use the dictionary tree to save all the values in the array, and then iterate the array from right to left, first remove the number of values from the tree, and then look for the number of elements in the tree that are smaller than *. * Worst time complexity: O (* * N) * Insert, Delete, find and use the tail return to speed up **/  Public classSolution { Public intReversepairs (int[] nums) {        intres = 0,lenn = nums.length;//, Halfmax = (integer.max_value)/2+10;        if(LENN&LT;2)return0; Node Head=NewNode (); Longtemp = nums[lenn-1]; //if (nums[lenn-1] < Halfmax)Addnum (Temp *2,head,63);  for(inti=lenn-2;i>-1;i--) {Temp=Nums[i]; Res+ = Getres (temp,head,63,0); Addnum (Temp*2,head,63); }        returnRes; }    Private voidDelnum (LongTar,node He,intbit) {        //Delete this element in the tree and update the value of the associated node        if(bit<0)return ; if(Tar & ((Long) (1) <<bit)) = = 0){            if(He.zero.num > 0) he.zero.num-= 1; //else return;he =He.zero; }Else{            if(He.one.num > 0) he.one.num-= 1; //else return;he =He.one; } delnum (Tar,he,bit-1); }    Private voidAddnum (LongTar,node He,intbit) {        //adds this element to the tree and updates the value of the associated node        if(bit<0)return; if(Tar & ((Long) (1) <<bit)) = = 0){            if(He.zero = =NULL) He.zero =NewNode (); ElseHe.zero.num + = 1; He=He.zero; }Else{            if(He.one = =NULL) He.one =NewNode (); ElseHe.one.num + = 1; He=He.one; } addnum (Tar,he,bit-1); }    Private intGetres (LongTar,node He,intBitintRes) {        //special judgment of sign bit        if(tar>=0){            if(He.one! =NULL) Res + =He.one.num; He=He.zero; }Else{He=He.one; }        returnGetsmallernum (tar,he,bit-1, RES); }    Private intGetsmallernum (LongTar,node He,intBitintRes) {        //gets the number of all the numbers that are smaller than the tar, which is said to be tail-recursive        if(Bit<0 | | he = =NULL)returnRes; if(Tar & ((Long) (1) <<bit)) = = 0) {He=He.zero; }Else{            if(He.zero! =NULL) Res + =He.zero.num; He=He.one; }        returnGetsmallernum (tar,he,bit-1, RES); }    Private Static classnode{ Public intnum;  PublicNode Zero,one;  PublicNode () {num= 1; Zero=NULL; One=NULL; }    }}

The ultimate solution for algorithms for integer array classes

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.