352. Data Stream as Disjoint Intervals

问题描述

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.

解题思路:

这道题是目前最新的题,其实思路很容易找到,难点在于考虑到所有的可能的情形。

首先要确定类必须有一个保存当前结果的集合类List<Interval>,其元素的按Interval的起始值的大小排序,当调用getIntervals()时直接返回该lists,当调用addNum(val)类时会对List<Interval>中的类进行调整。具体调整步骤如下:

1)调用自定义的findIndex(int val)方法找到Interval起始值小于val的最大的Interval的值。具体查找过程可以用二分查找的方法。这里需要考虑两个特殊情况:val值比位置为0的Interval的起始值还小;或者比最后一个Interval的起始值还大。这两个情况可以做特殊处理(分别返回-1,和lists.size()-1)。

2)针对返回值考虑如下情况(直接举具体的例子,相信大家都可以理解):

若返回值是-1:

a.若当前值为1,位置为0的Interval值为(2,x),可以修改这个Interval为(1,x);

b.若当前值为1,位置为0的Interval值为(3,x),可以新建一个Interval(1,1),加入lists中。

若返回值是lists.size()-1:

a.若当前值为11,最后一个Interval是(8,11),不做修改;

b.若当前值为11,最后一个Intever是(8,10),需要将它改为(8,11);

c.若当前值为11,最后一个Intever是(8,9),需要新建一个(11,11),加入lists中。

若返回值pos在[0,lists.size()-1);a.如果val==lists.get(pos+1).start-1 && val==lists.get(pos).end+1:需要将两个Interval合并成一个新的Interval;b.如果val==lists.get(pos+1).start-1 && val!=lists.get(pos).end+1:需要修改lists[pos+1]的值;c.如果val==lists.get(pos+1).start-1 && val==lists.get(pos).end+1需要修改lists[pos]的值;d.其他情况:新建一个Interval:(val,val)并加入到lists中。

具体代码:
  1 public class SummaryRanges {
  2
  3     List<Interval> lists;
  4     /** Initialize your data structure here. */
  5     public SummaryRanges() {
  6         lists=new ArrayList<Interval>();
  7
  8     }
  9
 10    public void addNum(int val) {
 11         if(lists.size()==0){
 12             Interval p = new Interval(val,val);
 13             lists.add(p);
 14             return;
 15         }
 16         if(lists.size()==1){
 17             if(lists.get(0).end <val){
 18                 Interval p=lists.remove(0);
 19                 if(p.end==val-1){
 20                     Interval p1=new Interval(p.start,val);
 21                     lists.add(p1);
 22                     return;
 23                 }
 24                 Interval p1=new Interval(val,val);
 25                 lists.add(p);
 26                 lists.add(p1);
 27
 28                 return;
 29             }
 30             else if(lists.get(0).start >val){
 31                 Interval p=lists.remove(0);
 32                 if(p.start==val+1){
 33                     Interval p1=new Interval(val,p.start);
 34                     lists.add(p1);
 35
 36                     return;
 37                 }
 38                 Interval p1=new Interval(val,val);
 39                 lists.add(p1);
 40                 lists.add(p);
 41
 42                 return;
 43             }
 44             else{
 45                 return;
 46             }
 47         }
 48         int pos=findIndex(val);
 49         if(pos==-1){
 50             if(val==lists.get(0).start-1){
 51                 lists.get(0).start=val;
 52                 return;
 53             }
 54             else{
 55                 Interval p1=new Interval(val,val);
 56                 lists.add(0, p1);
 57                 return;
 58             }
 59         }
 60         if(pos==lists.size()-1){
 61             if(val<=lists.get(pos).end){
 62                 return;
 63             }
 64             else{
 65                 if(val==lists.get(pos).end+1){
 66                     lists.get(pos).end=val;
 67                     return;
 68                 }
 69                 else{
 70                     Interval p1=new Interval(val,val);
 71                     lists.add(p1);
 72
 73                     return;
 74                 }
 75             }
 76         }
 77
 78         if(val<=lists.get(pos).end){
 79             return;
 80         }
 81         else if(val==lists.get(pos+1).start-1 && val==lists.get(pos).end+1){
 82             Interval p1=lists.remove(pos);
 83             Interval p2=lists.remove(pos);
 84             Interval p=new Interval(p1.start, p2.end);
 85
 86             lists.add(pos, p);
 87             return;
 88         }
 89         else if(val==lists.get(pos+1).start-1){
 90             lists.get(pos+1).start=val;
 91             return;
 92         }
 93         else if(val==lists.get(pos).end+1){
 94             lists.get(pos).end=val;
 95             return;
 96         }
 97         else{
 98             Interval p=new Interval(val, val);
 99             lists.add(pos+1, p);
100
101             return;
102         }
103
104     }
105
106     public List<Interval> getIntervals() {
107         return lists;
108     }
109
110     private int findIndex(int val){
111         int start=0;
112         int end=lists.size()-1;
113         if(lists.get(0).start>val)
114             return -1;
115         if(lists.get(lists.size()-1).start<val)
116             return lists.size()-1;
117         while(start<end){
118             int mid=(start+end)/2;
119             if(val<lists.get(mid).start){
120                 end=mid-1;
121             }
122             else if(val>lists.get(mid).start){
123                 if(val<lists.get(mid+1).start)
124                     return mid;
125                 else
126                     start= mid+1;
127             }
128             else
129                 return mid;
130         }
131         return start;
132     }
133 }
步骤可能有些繁琐,以后有时间会继续优化代码。
时间: 2024-08-01 10:46:02

352. Data Stream as Disjoint Intervals的相关文章

[email&#160;protected] [352] Data Stream as Disjoint Intervals (Binary Search &amp; TreeSet)

https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the da

352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

[leetcode]352. Data Stream as Disjoint Intervals

数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样提交了,发现错了,想到之前考虑要不要判重,我感觉是这个问题,然后就是在二分查找的时候,判断一下左右区间是否包含当前的值,包含就直接返回. 1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * in

352[LeetCode] Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

352. Data Stream as Disjoint Interval

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

[LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

Leetcode: Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De

【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法 一个是内置类型优先队列怎么设置小根堆(默认大根堆) 如果是自定义数据结构,有两种办法 1.定义这种数据结构的比较符号,就可以当成内置类型整 2.传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了 LRU Cache class LRUCache{ public: // @param capacity, an integer int Time; typedef i