# 352. Data Stream as disjoint intervals

Source: Internet
Author: User

Problem Description :

Given a data stream input of non-negative integers a1, A2, ..., summarize the numbers seen so far as a list of Di Sjoint intervals.

Problem Solving Ideas:

This problem is the latest problem, in fact, the idea is easy to find, the difficulty is to take into account all possible circumstances.

The first thing to determine is that the class must have a collection class List<interval> that holds the current result, and that its elements are sorted by the size of the starting value of the Interval, when the call to Getintervals () is returned directly to the lists when the Addnum (Val) Class is adjusted for classes in list<interval>. The specific adjustment steps are as follows:

1) Call the custom findindex (int val) method to find the value of the interval starting value that is less than the maximum interval of Val. The specific lookup process can be found using binary methods. Two special cases need to be considered: the Val value is smaller than the starting value of the interval with position 0, or greater than the starting value of the last interval. These two cases can be handled with special treatment (return 1, and lists.size () 1) respectively.

2) Consider the following for the return value (just give specific examples, I believe you can understand):

If the return value is-1:

A. If the current value is 1 and the interval value of position 0 is (2,x), this interval can be modified (1,x);

B. If the current value is 1 and the interval value at position 0 is (3,x), you can create a new interval () and join lists.

If the return value is Lists.size ()-1:

A. If the current value is 11, the last interval is (8,11), do not modify;

B. If the current value is 11, the last Intever is (8,10), it needs to be changed to (8,11);

C. If the current value is 11 and the last Intever is (8,9), you need to create a new (11,11) and join lists.

`If the return value of POS is in [0,lists.size ()-1);A. If Val==lists.get (pos+1). Start-1 && Val==lists.get (POS). End+1: Two interval need to be merged into a new interval;B. If Val==lists.get (pos+1). Start-1 && val! =lists.get (POS). End+1: Need to modify the value of lists[pos+1];C. If Val==lists.get (pos+1). Start-1 && Val==lists.get (POS). end+1 need to modify the value of Lists[pos];D. Other situation: Create a new interval: (Val,val) and add to lists.Specific code:`
`1  Public classSummaryranges {2     3List<interval>lists;4     /**Initialize your data structure here.*/5      Publicsummaryranges () {6lists=NewArraylist<interval>();7         8     }9     Ten     Public voidAddnum (intval) { One         if(Lists.size () ==0){ AInterval p =NewInterval (val,val); - Lists.add (p); -             return; the         } -         if(Lists.size () ==1){ -             if(Lists.get (0). End <val) { -Interval P=lists.remove (0); +                 if(P.END==VAL-1){ -Interval p1=NewInterval (p.start,val); + Lists.add (p1); A                     return; at                 } -Interval p1=NewInterval (val,val); - Lists.add (p); - Lists.add (p1); -                  -                 return; in             } -             Else if(Lists.get (0). Start >val) { toInterval P=lists.remove (0); +                 if(p.start==val+1){ -Interval p1=NewInterval (val,p.start); the Lists.add (p1); *   \$                     return;Panax Notoginseng                 } -Interval p1=NewInterval (val,val); the Lists.add (p1); + Lists.add (p); A                  the                 return; +             } -             Else{ \$                 return; \$             } -         } -         intpos=FindIndex (val); the         if(Pos==-1){ -             if(Val==lists.get (0). start-1){WuyiLists.get (0). start=Val; the                 return; -             } Wu             Else{ -Interval p1=NewInterval (val,val); AboutLists.add (0, p1); \$                 return; -             } -         } -         if(Pos==lists.size ()-1){ A             if(val<=Lists.get (POS). End) { +                 return; the             } -             Else{ \$                 if(Val==lists.get (POS). end+1){ theLists.get (POS). end=Val; the                     return; the                 } the                 Else{ -Interval p1=NewInterval (val,val); in Lists.add (p1); the                      the                     return; About                 } the             } the         } the          +         if(val<=Lists.get (POS). End) { -             return; the         }Bayi         Else if(Val==lists.get (pos+1). Start-1 && Val==lists.get (POS). end+1){ theInterval p1=Lists.remove (POS); theInterval p2=Lists.remove (POS); -Interval p=NewInterval (P1.start, p2.end); -          the Lists.add (POS, p); the             return; the         } the         Else if(Val==lists.get (pos+1). start-1){ -Lists.get (pos+1). start=Val; the             return; the         } the         Else if(Val==lists.get (POS). end+1){94Lists.get (POS). end=Val; the             return; the         } the         Else{98Interval p=NewInterval (Val, Val); AboutLists.add (pos+1, p); -             101             return;102         }103         104     } the     106      PublicList<interval>getintervals () {107         returnlists;108     }109      the     Private intFindIndex (intval) {111         intStart=0; the         intEnd=lists.size ()-1;113         if(Lists.get (0) .start>val) the             return-1; the         if(Lists.get (Lists.size ()-1) .start<val) the             returnLists.size ()-1;117          while(start<end) {118             intMid= (start+end)/2;119             if(val<Lists.get (mid). Start) { -End=mid-1;121             }122             Else if(val>Lists.get (mid). Start) {123                 if(Val<lists.get (mid+1). Start)124                     returnmid; the                 Else126Start= mid+1;127             } -             Else129                 returnmid; the         }131         returnstart; the     }133}`

`The steps may be cumbersome and will continue to optimize the code later.`

352. Data Stream as disjoint intervals

Related Keywords:

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.