|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 352. Data Stream as Disjoint Intervalues *- |
| 4 | +
|
| 5 | +Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervalues. |
| 6 | +
|
| 7 | +Implement the SummaryRanges class: |
| 8 | +
|
| 9 | +SummaryRanges() Initializes the object with an empty stream. |
| 10 | +void addNum(int valueue) Adds the integer valueue to the stream. |
| 11 | +int[][] getIntervalues() Returns a summary of the integers in the stream currently as a list of disjoint intervalues [startI, endI]. The answer should be sorted by startI. |
| 12 | + |
| 13 | +
|
| 14 | +Example 1: |
| 15 | +
|
| 16 | +Input |
| 17 | +["SummaryRanges", "addNum", "getIntervalues", "addNum", "getIntervalues", "addNum", "getIntervalues", "addNum", "getIntervalues", "addNum", "getIntervalues"] |
| 18 | +[[], [1], [], [3], [], [7], [], [2], [], [6], []] |
| 19 | +Output |
| 20 | +[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]]] |
| 21 | +
|
| 22 | +Explanation |
| 23 | +SummaryRanges summaryRanges = new SummaryRanges(); |
| 24 | +summaryRanges.addNum(1); // arr = [1] |
| 25 | +summaryRanges.getIntervalues(); // return [[1, 1]] |
| 26 | +summaryRanges.addNum(3); // arr = [1, 3] |
| 27 | +summaryRanges.getIntervalues(); // return [[1, 1], [3, 3]] |
| 28 | +summaryRanges.addNum(7); // arr = [1, 3, 7] |
| 29 | +summaryRanges.getIntervalues(); // return [[1, 1], [3, 3], [7, 7]] |
| 30 | +summaryRanges.addNum(2); // arr = [1, 2, 3, 7] |
| 31 | +summaryRanges.getIntervalues(); // return [[1, 3], [7, 7]] |
| 32 | +summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7] |
| 33 | +summaryRanges.getIntervalues(); // return [[1, 3], [6, 7]] |
| 34 | + |
| 35 | +
|
| 36 | +Constraints: |
| 37 | +
|
| 38 | +0 <= valueue <= 104 |
| 39 | +At most 3 * 104 calls will be made to addNum and getIntervalues. |
| 40 | + |
| 41 | +
|
| 42 | +Follow up: What if there are lots of merges and the number of disjoint intervalues is small compared to the size of the data stream? |
| 43 | +
|
| 44 | +
|
| 45 | +*/ |
| 46 | +/* |
| 47 | +
|
| 48 | +
|
| 49 | +
|
| 50 | +class SummaryRanges { |
| 51 | +
|
| 52 | + SummaryRanges() { |
| 53 | +
|
| 54 | + } |
| 55 | + |
| 56 | + void addNum(int valueue) { |
| 57 | +
|
| 58 | + } |
| 59 | + |
| 60 | + List<List<int>> getIntervalues() { |
| 61 | +
|
| 62 | + } |
| 63 | +} |
| 64 | +
|
| 65 | +
|
| 66 | +*/ |
| 67 | + |
| 68 | +import 'dart:collection'; |
| 69 | + |
| 70 | +// class SummaryRanges { |
| 71 | +// late HashSet<int> dataSet; |
| 72 | +// SummaryRanges() { |
| 73 | +// this.dataSet = HashSet(); |
| 74 | +// } |
| 75 | + |
| 76 | +// void addNum(int valueue) { |
| 77 | +// dataSet.add(valueue); |
| 78 | +// } |
| 79 | + |
| 80 | +// List<List<int>> getIntervalues() { |
| 81 | +// List<List<int>> intervalues = <int>[].map((e) => <int>[]).toList(); |
| 82 | + |
| 83 | +// int startI = -1, endI = -1; |
| 84 | +// for (int n in dataSet) { |
| 85 | +// if (startI == -1) { |
| 86 | +// startI = n; |
| 87 | +// endI = n; |
| 88 | +// } else if (n == endI + 1) { |
| 89 | +// endI = n; |
| 90 | +// } else { |
| 91 | +// intervalues.add([startI, endI]); |
| 92 | +// endI = n; |
| 93 | +// startI = n; |
| 94 | +// } |
| 95 | +// } |
| 96 | +// intervalues.add([startI, endI]); |
| 97 | +// return intervalues; |
| 98 | +// } |
| 99 | +// } |
| 100 | + |
| 101 | +class SummaryRanges { |
| 102 | + HashMap<int, int> front = HashMap(); |
| 103 | + HashMap<int, int> back = HashMap(); |
| 104 | + HashSet<int> set = HashSet(); |
| 105 | + SummaryRanges() {} |
| 106 | + |
| 107 | + void addNum(int value) { |
| 108 | + if (set.contains(value)) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (back.containsKey(value - 1) && front.containsKey(value + 1)) { |
| 113 | + int st = back[value - 1] ?? 0; |
| 114 | + int end = front[value + 1] ?? 0; |
| 115 | + front.remove(value + 1); |
| 116 | + front.remove(st); |
| 117 | + back.remove(end); |
| 118 | + back.remove(value - 1); |
| 119 | + front[st] = end; |
| 120 | + back[end] = st; |
| 121 | + } else if (front.containsKey(value + 1)) { |
| 122 | + int end = front[value + 1] ?? 0; |
| 123 | + front.remove(value + 1); |
| 124 | + back.remove(end); |
| 125 | + front[value] = end; |
| 126 | + back[end] = value; |
| 127 | + } else if (back.containsKey(value - 1)) { |
| 128 | + int st = back[value - 1] ?? 0; |
| 129 | + front.remove(st); |
| 130 | + back.remove(value - 1); |
| 131 | + front[st] = value; |
| 132 | + back[value] = st; |
| 133 | + } else { |
| 134 | + front[value] = value; |
| 135 | + back[value] = value; |
| 136 | + } |
| 137 | + set.add(value); |
| 138 | + } |
| 139 | + |
| 140 | + List<List<int>> getIntervalues() { |
| 141 | + int n = front.length; |
| 142 | + List<List<int>> ans = |
| 143 | + List.filled(n, 0).map((e) => List.filled(2, 0)).toList(); |
| 144 | + int i = 0; |
| 145 | + for (MapEntry<int, int> e in front.entries) { |
| 146 | + int p = e.key; |
| 147 | + int q = e.value; |
| 148 | + ans[i][0] = p; |
| 149 | + ans[i][1] = q; |
| 150 | + i++; |
| 151 | + } |
| 152 | + return ans; |
| 153 | + } |
| 154 | +} |
0 commit comments