Skip to content

Commit 11d4804

Browse files
committed
Add solution #352
1 parent 730cfe6 commit 11d4804

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
347|[Top K Frequent Elements](./0347-top-k-frequent-elements.js)|Medium|
236236
349|[Intersection of Two Arrays](./0349-intersection-of-two-arrays.js)|Easy|
237237
350|[Intersection of Two Arrays II](./0350-intersection-of-two-arrays-ii.js)|Easy|
238+
352|[Data Stream as Disjoint Intervals](./0352-data-stream-as-disjoint-intervals.js)|Hard|
238239
367|[Valid Perfect Square](./0367-valid-perfect-square.js)|Easy|
239240
371|[Sum of Two Integers](./0371-sum-of-two-integers.js)|Medium|
240241
372|[Super Pow](./0372-super-pow.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 352. Data Stream as Disjoint Intervals
3+
* https://leetcode.com/problems/data-stream-as-disjoint-intervals/
4+
* Difficulty: Hard
5+
*
6+
* Given a data stream input of non-negative integers a1, a2, ..., an, summarize the
7+
* numbers seen so far as a list of disjoint intervals.
8+
*
9+
* Implement the SummaryRanges class:
10+
* - SummaryRanges() Initializes the object with an empty stream.
11+
* - void addNum(int value) Adds the integer value to the stream.
12+
* - int[][] getIntervals() Returns a summary of the integers in the stream currently as
13+
* a list of disjoint intervals [starti, endi]. The answer should be sorted by starti.
14+
*/
15+
16+
var SummaryRanges = function() {
17+
this.list = [];
18+
};
19+
20+
/**
21+
* @param {number} value
22+
* @return {void}
23+
*/
24+
SummaryRanges.prototype.addNum = function(value) {
25+
this.list[value] = true;
26+
};
27+
28+
/**
29+
* @return {number[][]}
30+
*/
31+
SummaryRanges.prototype.getIntervals = function() {
32+
const result = [];
33+
34+
for (let i = 0; i < this.list.length; i++) {
35+
if (this.list[i]) {
36+
let j = i;
37+
while (this.list[j]) {
38+
j++;
39+
}
40+
result.push([i, j - 1]);
41+
i = j;
42+
}
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)