Skip to content

Commit 89498fb

Browse files
committed
+ problem 352
1 parent 3037fac commit 89498fb

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 352. Data Stream as Disjoint Intervals
2+
Given a data stream input of non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code>, summarize the numbers seen so far as a list of disjoint intervals.
3+
4+
Implement the `SummaryRanges` class:
5+
6+
* `SummaryRanges()` Initializes the object with an empty stream.
7+
* `void addNum(int value)` Adds the integer `value` to the stream.
8+
* `int[][] getIntervals()` Returns a summary of the integers in the stream currently as a list of disjoint intervals <code>[start<sub>i</sub>, end<sub>i</sub>]</code>. The answer should be sorted by <code>start<sub>i</sub></code>.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong>
13+
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
14+
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
15+
<strong>Output:</strong>
16+
[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]]]
17+
<strong>Explanation:</strong>
18+
SummaryRanges summaryRanges = new SummaryRanges();
19+
summaryRanges.addNum(1); // arr = [1]
20+
summaryRanges.getIntervals(); // return [[1, 1]]
21+
summaryRanges.addNum(3); // arr = [1, 3]
22+
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
23+
summaryRanges.addNum(7); // arr = [1, 3, 7]
24+
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
25+
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
26+
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
27+
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
28+
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
29+
</pre>
30+
31+
#### Constraints:
32+
* <code>0 <= value <= 10<sup>4</sup></code>
33+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `addNum` and `getIntervals`.
34+
* At most <code>10<sup>2</sup></code> calls will be made to `getIntervals`.
35+
36+
**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?
37+
38+
## Solutions (Python)
39+
40+
### 1. Solution
41+
```Python
42+
from sortedcontainers import SortedList
43+
44+
45+
class SummaryRanges:
46+
47+
def __init__(self):
48+
self.intervals = SortedList()
49+
self.nums = set()
50+
51+
def addNum(self, value: int) -> None:
52+
if value in self.nums:
53+
return
54+
55+
i = self.intervals.bisect_left([value, value])
56+
self.nums.add(value)
57+
58+
if value - 1 in self.nums and value + 1 in self.nums:
59+
x, _ = self.intervals.pop(i - 1)
60+
_, y = self.intervals.pop(i - 1)
61+
self.intervals.add([x, y])
62+
elif value - 1 in self.nums:
63+
x, _ = self.intervals.pop(i - 1)
64+
self.intervals.add([x, value])
65+
elif value + 1 in self.nums:
66+
_, y = self.intervals.pop(i)
67+
self.intervals.add([value, y])
68+
else:
69+
self.intervals.add([value, value])
70+
71+
def getIntervals(self) -> List[List[int]]:
72+
return list(self.intervals)
73+
74+
75+
# Your SummaryRanges object will be instantiated and called as such:
76+
# obj = SummaryRanges()
77+
# obj.addNum(value)
78+
# param_2 = obj.getIntervals()
79+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 352. 将数据流变为多个不相交区间
2+
给你一个由非负整数 <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。
3+
4+
实现 `SummaryRanges` 类:
5+
6+
* `SummaryRanges()` 使用一个空数据流初始化对象。
7+
* `void addNum(int val)` 向数据流中加入整数 `val`
8+
* `int[][] getIntervals()` 以不相交区间 <code>[start<sub>i</sub>, end<sub>i</sub>]</code> 的列表形式返回对数据流中整数的总结。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong>
13+
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
14+
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
15+
<strong>输出:</strong>
16+
[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]]]
17+
<strong>解释:</strong>
18+
SummaryRanges summaryRanges = new SummaryRanges();
19+
summaryRanges.addNum(1); // arr = [1]
20+
summaryRanges.getIntervals(); // 返回 [[1, 1]]
21+
summaryRanges.addNum(3); // arr = [1, 3]
22+
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
23+
summaryRanges.addNum(7); // arr = [1, 3, 7]
24+
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
25+
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
26+
summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
27+
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
28+
summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
29+
</pre>
30+
31+
#### 提示:
32+
* <code>0 <= value <= 10<sup>4</sup></code>
33+
* 最多调用 `addNum``getIntervals` 方法 <code>3 * 10<sup>4</sup></code> 次
34+
35+
**进阶:**如果存在大量合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?
36+
37+
## 题解 (Python)
38+
39+
### 1. 题解
40+
```Python
41+
from sortedcontainers import SortedList
42+
43+
44+
class SummaryRanges:
45+
46+
def __init__(self):
47+
self.intervals = SortedList()
48+
self.nums = set()
49+
50+
def addNum(self, value: int) -> None:
51+
if value in self.nums:
52+
return
53+
54+
i = self.intervals.bisect_left([value, value])
55+
self.nums.add(value)
56+
57+
if value - 1 in self.nums and value + 1 in self.nums:
58+
x, _ = self.intervals.pop(i - 1)
59+
_, y = self.intervals.pop(i - 1)
60+
self.intervals.add([x, y])
61+
elif value - 1 in self.nums:
62+
x, _ = self.intervals.pop(i - 1)
63+
self.intervals.add([x, value])
64+
elif value + 1 in self.nums:
65+
_, y = self.intervals.pop(i)
66+
self.intervals.add([value, y])
67+
else:
68+
self.intervals.add([value, value])
69+
70+
def getIntervals(self) -> List[List[int]]:
71+
return list(self.intervals)
72+
73+
74+
# Your SummaryRanges object will be instantiated and called as such:
75+
# obj = SummaryRanges()
76+
# obj.addNum(value)
77+
# param_2 = obj.getIntervals()
78+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class SummaryRanges:
5+
6+
def __init__(self):
7+
self.intervals = SortedList()
8+
self.nums = set()
9+
10+
def addNum(self, value: int) -> None:
11+
if value in self.nums:
12+
return
13+
14+
i = self.intervals.bisect_left([value, value])
15+
self.nums.add(value)
16+
17+
if value - 1 in self.nums and value + 1 in self.nums:
18+
x, _ = self.intervals.pop(i - 1)
19+
_, y = self.intervals.pop(i - 1)
20+
self.intervals.add([x, y])
21+
elif value - 1 in self.nums:
22+
x, _ = self.intervals.pop(i - 1)
23+
self.intervals.add([x, value])
24+
elif value + 1 in self.nums:
25+
_, y = self.intervals.pop(i)
26+
self.intervals.add([value, y])
27+
else:
28+
self.intervals.add([value, value])
29+
30+
def getIntervals(self) -> List[List[int]]:
31+
return list(self.intervals)
32+
33+
34+
# Your SummaryRanges object will be instantiated and called as such:
35+
# obj = SummaryRanges()
36+
# obj.addNum(value)
37+
# param_2 = obj.getIntervals()

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
[347][347l] |[Top K Frequent Elements][347] |![rs]
214214
[349][349l] |[Intersection of Two Arrays][349] |![rs]
215215
[350][350l] |[Intersection of Two Arrays II][350] |![rs]
216+
[352][352l] |[Data Stream as Disjoint Intervals][352] |![py]
216217
[357][357l] |[Count Numbers with Unique Digits][357] |![rs]
217218
[365][365l] |[Water and Jug Problem][365] |![rb]
218219
[367][367l] |[Valid Perfect Square][367] |![rs]
@@ -1496,6 +1497,7 @@
14961497
[347]:Problemset/0347-Top%20K%20Frequent%20Elements/README.md#347-top-k-frequent-elements
14971498
[349]:Problemset/0349-Intersection%20of%20Two%20Arrays/README.md#349-intersection-of-two-arrays
14981499
[350]:Problemset/0350-Intersection%20of%20Two%20Arrays%20II/README.md#350-intersection-of-two-arrays-ii
1500+
[352]:Problemset/0352-Data%20Stream%20as%20Disjoint%20Intervals/README.md#352-data-stream-as-disjoint-intervals
14991501
[357]:Problemset/0357-Count%20Numbers%20with%20Unique%20Digits/README.md#357-count-numbers-with-unique-digits
15001502
[365]:Problemset/0365-Water%20and%20Jug%20Problem/README.md#365-water-and-jug-problem
15011503
[367]:Problemset/0367-Valid%20Perfect%20Square/README.md#367-valid-perfect-square
@@ -2778,6 +2780,7 @@
27782780
[347l]:https://leetcode.com/problems/top-k-frequent-elements/
27792781
[349l]:https://leetcode.com/problems/intersection-of-two-arrays/
27802782
[350l]:https://leetcode.com/problems/intersection-of-two-arrays-ii/
2783+
[352l]:https://leetcode.com/problems/data-stream-as-disjoint-intervals/
27812784
[357l]:https://leetcode.com/problems/count-numbers-with-unique-digits/
27822785
[365l]:https://leetcode.com/problems/water-and-jug-problem/
27832786
[367l]:https://leetcode.com/problems/valid-perfect-square/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
[347][347l] |[前 K 个高频元素][347] |![rs]
214214
[349][349l] |[两个数组的交集][349] |![rs]
215215
[350][350l] |[两个数组的交集 II][350] |![rs]
216+
[352][352l] |[将数据流变为多个不相交区间][352] |![py]
216217
[357][357l] |[计算各个位数不同的数字个数][357] |![rs]
217218
[365][365l] |[水壶问题][365] |![rb]
218219
[367][367l] |[有效的完全平方数][367] |![rs]
@@ -1496,6 +1497,7 @@
14961497
[347]:Problemset/0347-Top%20K%20Frequent%20Elements/README_CN.md#347-前-k-个高频元素
14971498
[349]:Problemset/0349-Intersection%20of%20Two%20Arrays/README_CN.md#349-两个数组的交集
14981499
[350]:Problemset/0350-Intersection%20of%20Two%20Arrays%20II/README_CN.md#350-两个数组的交集-ii
1500+
[352]:Problemset/0352-Data%20Stream%20as%20Disjoint%20Intervals/README_CN.md#352-将数据流变为多个不相交区间
14991501
[357]:Problemset/0357-Count%20Numbers%20with%20Unique%20Digits/README_CN.md#357-计算各个位数不同的数字个数
15001502
[365]:Problemset/0365-Water%20and%20Jug%20Problem/README_CN.md#365-水壶问题
15011503
[367]:Problemset/0367-Valid%20Perfect%20Square/README_CN.md#367-有效的完全平方数
@@ -2778,6 +2780,7 @@
27782780
[347l]:https://leetcode.cn/problems/top-k-frequent-elements/
27792781
[349l]:https://leetcode.cn/problems/intersection-of-two-arrays/
27802782
[350l]:https://leetcode.cn/problems/intersection-of-two-arrays-ii/
2783+
[352l]:https://leetcode.cn/problems/data-stream-as-disjoint-intervals/
27812784
[357l]:https://leetcode.cn/problems/count-numbers-with-unique-digits/
27822785
[365l]:https://leetcode.cn/problems/water-and-jug-problem/
27832786
[367l]:https://leetcode.cn/problems/valid-perfect-square/

0 commit comments

Comments
 (0)