aboutsummaryrefslogtreecommitdiff
path: root/other/data_stream_disjoint_interval.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/data_stream_disjoint_interval.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/data_stream_disjoint_interval.cpp')
-rw-r--r--other/data_stream_disjoint_interval.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/other/data_stream_disjoint_interval.cpp b/other/data_stream_disjoint_interval.cpp
new file mode 100644
index 0000000..3682ee2
--- /dev/null
+++ b/other/data_stream_disjoint_interval.cpp
@@ -0,0 +1,45 @@
+// 352. Data Stream as Disjoint Intervals
+#include "leetcode.h"
+
+/*
+ * given a data stream input of non-negative integers 'a1, a2, ..., an',
+ * summarize the numbers seen so far as a list of disjoint intervals. implement
+ * 'SummaryRanges': SummaryRanges() initialises the object with an empty stream
+ * void addNum(int value) adds the integer 'value' to the stream
+ * int[][] getIntervals() returns a summary of the integers in the stream
+ * currently as a list of disjoint intervals '[starti, endi]'. sort answer by
+ * 'starti'
+ */
+
+class SummaryRanges {
+public:
+ map<int, int> m;
+ void addNum(int value) {
+ if (m.count(value))
+ return;
+ m[value] = value;
+ int mn = value, mx = value;
+ if (m.count(value - 1))
+ mn = m[value - 1];
+ if (m.count(value + 1))
+ mx = m[value + 1];
+ m[mn] = mx;
+ m[mx] = mn;
+ }
+ vvd getIntervals() {
+ vvd ans;
+ auto it = m.begin();
+ while (it != m.end()) {
+ ans.push_back({it->first, it->second});
+ it = m.upper_bound(it->second);
+ }
+ return ans;
+ }
+};
+
+int main() {
+ SummaryRanges *obj = new SummaryRanges();
+ int value;
+ obj->addNum(value);
+ vvd param_2 = obj->getIntervals();
+}