RainX_69

MERGE Data Stream as Disjoint Intervals (IMPORTANT)

Jan 29th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | Source Code | 0 0
  1. https://leetcode.com/problems/data-stream-as-disjoint-intervals/
  2.  
  3. Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.
  4.  
  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.  
  20. Explanation
  21. SummaryRanges summaryRanges = new SummaryRanges();
  22. summaryRanges.addNum(1);      // arr = [1]
  23. summaryRanges.getIntervals(); // return [[1, 1]]
  24. summaryRanges.addNum(3);      // arr = [1, 3]
  25. summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
  26. summaryRanges.addNum(7);      // arr = [1, 3, 7]
  27. summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
  28. summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
  29. summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
  30. summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
  31. summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
  32.  
  33.  
  34. Constraints:
  35.  
  36. 0 <= value <= 10^4
  37. At most 3 * 10^4 calls will be made to addNum and getIntervals.
  38.  
  39.  
  40. 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?
  41.  
  42.  
  43. ---------------------------------------------------------------------------------------------------------------------------------------
  44.  
  45. class SummaryRanges {
  46. private:
  47.     map<int,int> ranges;  
  48. public:
  49.     SummaryRanges() {
  50.        
  51.     }
  52.    
  53.     void addNum(int value) {
  54.         auto itr=ranges.upper_bound(value);
  55.         int start=value;
  56.         int end=value;
  57.         if(itr!=ranges.begin()){
  58.             auto it=prev(itr);
  59.             if(it->second>=value){ // already included in the interval
  60.                 return;
  61.             }
  62.             if(it->second+1==value){ // in this interval, value can attach itself as end, so start gets updated
  63.                 start=it->first;
  64.                 ranges.erase(it);
  65.             }
  66.         }
  67.         if(itr!=ranges.end() && itr->first-1==value){ // in this interval, value can attach itself as start, so end gets updated
  68.             end=itr->second;
  69.             ranges.erase(itr);
  70.         }
  71.         ranges[start]=end;        
  72.     }
  73.    
  74.     vector<vector<int>> getIntervals() {
  75.         vector<vector<int>> intervals;
  76.         for(auto interval: ranges){
  77.             intervals.push_back({interval.first,interval.second});
  78.         }
  79.         return intervals;
  80.     }
  81. };
  82.  
Advertisement
Add Comment
Please, Sign In to add comment