352. Data Stream as Disjoint Intervals Hard

@problem@discussion
#Binary Search#Design#Ordered Set



1/**
2 * [352] Data Stream as Disjoint Intervals
3 *
4 * Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.
5 * Implement the SummaryRanges class:
6 * 
7 * 	SummaryRanges() Initializes the object with an empty stream.
8 * 	void addNum(int value) Adds the integer value to the stream.
9 * 	int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [starti, endi]. The answer should be sorted by starti.
10 * 
11 *  
12 * Example 1:
13 * 
14 * Input
15 * ["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
16 * [[], [1], [], [3], [], [7], [], [2], [], [6], []]
17 * Output
18 * [null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
19 * Explanation
20 * SummaryRanges summaryRanges = new SummaryRanges();
21 * summaryRanges.addNum(1);      // arr = [1]
22 * summaryRanges.getIntervals(); // return [[1, 1]]
23 * summaryRanges.addNum(3);      // arr = [1, 3]
24 * summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
25 * summaryRanges.addNum(7);      // arr = [1, 3, 7]
26 * summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
27 * summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
28 * summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
29 * summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
30 * summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	0 <= value <= 10^4
36 * 	At most 3 * 10^4 calls will be made to addNum and getIntervals.
37 * 
38 *  
39 * Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/data-stream-as-disjoint-intervals/
45// discuss: https://leetcode.com/problems/data-stream-as-disjoint-intervals/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49struct SummaryRanges {
50        false
51    }
52
53
54/** 
55 * `&self` means the method takes an immutable reference.
56 * If you need a mutable reference, change it to `&mut self` instead.
57 */
58impl SummaryRanges {
59
60    fn new() -> Self {
61        
62    }
63    
64    fn add_num(&self, value: i32) {
65        
66    }
67    
68    fn get_intervals(&self) -> Vec<Vec<i32>> {
69        
70    }
71}
72
73/**
74 * Your SummaryRanges object will be instantiated and called as such:
75 * let obj = SummaryRanges::new();
76 * obj.add_num(value);
77 * let ret_2: Vec<Vec<i32>> = obj.get_intervals();
78 */
79
80// submission codes end
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_352() {
88    }
89}
90


Back
© 2025 bowen.ge All Rights Reserved.