Skip to content

Commit 32ebd5c

Browse files
committed
leetcode
1 parent dcc9c2e commit 32ebd5c

File tree

4 files changed

+297
-0
lines changed

4 files changed

+297
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
type SummaryRanges struct {
4+
nums []bool
5+
}
6+
7+
func Constructor() SummaryRanges {
8+
return SummaryRanges{
9+
nums: make([]bool, 10002),
10+
}
11+
}
12+
13+
func (this *SummaryRanges) AddNum(value int) {
14+
this.nums[value] = true
15+
}
16+
17+
func (this *SummaryRanges) GetIntervals() [][]int {
18+
res := make([][]int, 0)
19+
lo, hi := -1, -1
20+
for i := range this.nums {
21+
if !this.nums[i] {
22+
if lo != -1 {
23+
res = append(res, []int{lo, hi})
24+
lo, hi = -1, -1
25+
}
26+
continue
27+
}
28+
if lo == -1 {
29+
lo, hi = i, i
30+
continue
31+
}
32+
hi = i
33+
}
34+
return res
35+
36+
}
37+
38+
// O(N) O(1)
39+
// O(1) O(N)
40+
41+
/**
42+
* Your SummaryRanges object will be instantiated and called as such:
43+
* obj := Constructor();
44+
* obj.AddNum(value);
45+
* param_2 := obj.GetIntervals();
46+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 🔥 100% FAST 🔥 || Simple Fast and Easy || with Explanation
2+
3+
```dart
4+
import 'dart:collection';
5+
6+
class SummaryRanges {
7+
HashMap<int, int> front = HashMap();
8+
HashMap<int, int> back = HashMap();
9+
HashSet<int> set = HashSet();
10+
SummaryRanges() {}
11+
12+
void addNum(int value) {
13+
if (set.contains(value)) {
14+
return;
15+
}
16+
17+
if (back.containsKey(value - 1) && front.containsKey(value + 1)) {
18+
int st = back[value - 1] ?? 0;
19+
int end = front[value + 1] ?? 0;
20+
front.remove(value + 1);
21+
front.remove(st);
22+
back.remove(end);
23+
back.remove(value - 1);
24+
front[st] = end;
25+
back[end] = st;
26+
} else if (front.containsKey(value + 1)) {
27+
int end = front[value + 1] ?? 0;
28+
front.remove(value + 1);
29+
back.remove(end);
30+
front[value] = end;
31+
back[end] = value;
32+
} else if (back.containsKey(value - 1)) {
33+
int st = back[value - 1] ?? 0;
34+
front.remove(st);
35+
back.remove(value - 1);
36+
front[st] = value;
37+
back[value] = st;
38+
} else {
39+
front[value] = value;
40+
back[value] = value;
41+
}
42+
set.add(value);
43+
}
44+
45+
List<List<int>> getIntervals() {
46+
int n = front.length;
47+
List<List<int>> ans =
48+
List.filled(n, 0).map((e) => List.filled(2, 0)).toList();
49+
int i = 0;
50+
for (MapEntry<int, int> e in front.entries) {
51+
int p = e.key;
52+
int q = e.value;
53+
ans[i][0] = p;
54+
ans[i][1] = q;
55+
i++;
56+
}
57+
return ans;
58+
}
59+
}
60+
61+
```
62+
63+
```dart
64+
import 'dart:collection';
65+
66+
class SummaryRanges {
67+
late HashSet<int> dataSet;
68+
SummaryRanges() {
69+
this.dataSet = HashSet();
70+
}
71+
72+
void addNum(int value) {
73+
dataSet.add(value);
74+
}
75+
76+
List<List<int>> getIntervals() {
77+
List<List<int>> intervals = <int>[].map((e) => <int>[]).toList();
78+
79+
int startI = -1, endI = -1;
80+
for (int n in dataSet) {
81+
if (startI == -1) {
82+
startI = n;
83+
endI = n;
84+
} else if (n == endI + 1) {
85+
endI = n;
86+
} else {
87+
intervals.add([startI, endI]);
88+
endI = n;
89+
startI = n;
90+
}
91+
}
92+
intervals.add([startI, endI]);
93+
return intervals;
94+
}
95+
}
96+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
196196
- [**2359.** Find Closest Node to Given Two Nodes](FindClosestNodeToGivenTwoNodes/find_closest_node_to_given_two_nodes.dart)
197197
- [**787.** Cheapest Flights Within K Stops](CheapestFlightsWithinKStops/cheapest_flights_within_k_stops.dart)
198198
- [**472.** Concatenated Words](ConcatenatedWords/concatenated_words.dart)
199+
- [**352.** Data Stream as Disjoint Intervals](DataStreamAsDisjointIntervals/data_stream_as_disjoint_intervals.dart)
199200

200201
## Reach me via
201202

0 commit comments

Comments
 (0)