Maintain real-time median and rank

Source: Internet
Author: User

1. Real-time median leecode 295

/** Solution: * 1. Use Priorityqueue to create a new two heap, a large root heap maxheap (need to implement the comparator), a small Gan minheap * 2. When inserting an element, let the top element of the Dagen always be less than or equal to the median number. The heap top element of the Gan is always greater than the median * 3. So the number of elements is an odd number, Gens more than one element, and the median is a large heap heap top element, the number of elements is even, two heaps as high, the median is the average of two heap of top elements * 4. When inserting elements, To compare the top elements of a heap against a two-heap size, decide which heap to insert*/ Public classS295 {PrivatePriorityqueue<integer> maxheap =NewPriorityqueue<integer> (Newmaxcomparator ()); PrivatePriorityqueue<integer> minheap =NewPriorityqueue<integer>(); //Adds A number into the data structure.     Public voidAddnum (intnum) {        if(Maxheap.size () >minheap.size ()) {            if(Num <Maxheap.peek ())                {Minheap.offer (Maxheap.poll ());            Maxheap.offer (num); } Else{minheap.offer (num); }        } Else {            if(maxheap.size () = = 0) {maxheap.offer (num); } Else {                if(Num >Minheap.peek ())                    {Maxheap.offer (Minheap.poll ());                Minheap.offer (num); } Else{maxheap.offer (num); }            }        }    }    //Returns The median of the current data stream     Public DoubleFindmedian () {if(maxheap.size () = = 0)            return-1; if(maxheap.size () = =minheap.size ()) {            return(Double) (Maxheap.peek () + Minheap.peek ())/2; } Else {            returnMaxheap.peek (); }    }}classMaxcomparatorImplementsComparator<integer>{@Override Public intCompare (integer A, integer b) {if(A >b) {return-1; } Else if(A <b) {return1; } Else {            return0; }    }}
View Code

2. The rank of a number in the real-time rank is the number of numbers less than or equal to this number in the current array programmer interview Gold P267 11-8

* Solution:
* Maintenance of real-time rank is actually to make the array always orderly, it is easy to think of the two-fork search tree, but to each node in real-time record the number of left child nodes leftsize.
* We start looking for the root node of the two-fork lookup tree and there are three scenarios
* 1. The current node value equals the current value, then the rank of the current value is the leftsize of the current node
* 2. If the current node value is greater than the current value, continue searching toward the left child node of the current node
* 3. The current node value is less than the current value, the rank of the current value is first added to the current node value of Leftsize + 1 (1 for the current node itself), and then to the right to continue to find

ImportJava.util.*; Public classRank {ranknode root=NULL;  Public int[] Getrankofnumber (int[] A,intN) {//Write code here        int[] Rank =New int[n];  for(inti = 0; I < n; i++) {track (a[i]); Rank[i]=Root.getrank (A[i]); }        returnrank; }         Public voidTrackintval) {        if(Root = =NULL) Root=NewRanknode (Val); ElseRoot.insert (Val); }}classRanknode {Ranknode Left=NULL, right =NULL; intval = 0; intLeftsize = 0;  PublicRanknode (intval) {         This. val =Val; }     Public voidInsertintval) {        if(Val <= This. val) {            if( This. Left! =NULL)                 This. Left.insert (Val); Else                 This. left =NewRanknode (Val);  This. leftsize++; } Else {            if( This. Right! =NULL)                 This. Right.insert (Val); Else                 This. right =NewRanknode (Val); }    }     Public intGetRank (intval) {        if(val = = This. val)return  This. leftsize; if(Val > This. val) {            if( This. right = =NULL)                return-1; return  This. leftsize + 1 + This. Right.getrank (Val); }        if( This. left = =NULL)            return-1; return  This. Left.getrank (Val); }}
View Code

Maintain real-time median and rank

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