diff --git a/go/0352-data-stream-as-disjoint-intervals.go b/go/0352-data-stream-as-disjoint-intervals.go new file mode 100644 index 000000000..c57bc225d --- /dev/null +++ b/go/0352-data-stream-as-disjoint-intervals.go @@ -0,0 +1,44 @@ +package main + +import "sort" + +type SummaryRanges struct { + numSet map[int]bool +} + +func Constructor() SummaryRanges { + return SummaryRanges{numSet: map[int]bool{}} +} + +func (this *SummaryRanges) AddNum(value int) { + this.numSet[value] = true +} + +func (this *SummaryRanges) GetIntervals() [][]int { + nums := []int{} + for num, _ := range this.numSet { + nums = append(nums, num) + } + sort.Ints(nums) + + res := [][]int{} + + i := 0 + + for i < len(nums) { + start := nums[i] + + for i +1 < len(nums) && nums[i] +1 == nums[i+1] { + i++ + } + + res = append(res, []int{start, nums[i]}) + i++ + } + + return res +} + +func main() { + +} \ No newline at end of file diff --git a/javascript/0352-data-stream-as-disjoint-intervals.js b/javascript/0352-data-stream-as-disjoint-intervals.js new file mode 100644 index 000000000..3cd7f7717 --- /dev/null +++ b/javascript/0352-data-stream-as-disjoint-intervals.js @@ -0,0 +1,31 @@ +class SummaryRanges { + constructor() { + this.numSet = new Set(); + } + + addNum(value) { + this.numSet.add(value); + } + + getIntervals() { + let nums = Array.from(this.numSet.keys()); + nums.sort((a, b) => a - b); + + let res = []; + + let i = 0; + + while (i < nums.length) { + let start = nums[i]; + + while (i + 1 < nums.length && nums[i] + 1 == nums[i + 1]) { + i++; + } + + res.push([start, nums[i]]); + i++; + } + + return res; + } +} diff --git a/rust/0352-data-stream-as-disjoint-intervals..rs b/rust/0352-data-stream-as-disjoint-intervals..rs new file mode 100644 index 000000000..a07f8fc4c --- /dev/null +++ b/rust/0352-data-stream-as-disjoint-intervals..rs @@ -0,0 +1,73 @@ +// BTreeSet Solution + +use std::collections::BTreeSet; + +struct SummaryRanges { + tree_map: BTreeSet, +} + +impl SummaryRanges { + fn new() -> Self { + Self { + tree_map: BTreeSet::new(), + } + } + + fn add_num(&mut self, value: i32) { + self.tree_map.insert(value); + } + + fn get_intervals(&self) -> Vec> { + let mut res: Vec> = vec![]; + + for n in &self.tree_map { + if !res.is_empty() && res.last().unwrap()[1] + 1 == *n { + let mut last = res.pop().unwrap(); + last[1] = *n; + res.push(last); + } else { + res.push(vec![*n, *n]); + } + } + + res + } +} + +// HashSet Solution +use std::collections::HashSet; +struct SummaryRanges { + ranges: Vec>, + num_set: HashSet, +} + +impl SummaryRanges { + fn new() -> Self { + Self { + ranges: vec![], + num_set: HashSet::new(), + } + } + + fn add_num(&mut self, value: i32) { + self.num_set.insert(value); + } + + fn get_intervals(&self) -> Vec> { + let mut nums: Vec = self.num_set.iter().cloned().collect(); + nums.sort(); + let mut res = vec![]; + let mut i = 0; + + while i < nums.len() { + let start = nums[i]; + + while i + 1 < nums.len() && nums[i] + 1 == nums[i + 1] { + i += 1; + } + res.push(vec![start, nums[i]]); + i += 1; + } + res + } +} diff --git a/typescript/0352-data-stream-as-disjoint-intervals..ts b/typescript/0352-data-stream-as-disjoint-intervals..ts new file mode 100644 index 000000000..cfc6e1942 --- /dev/null +++ b/typescript/0352-data-stream-as-disjoint-intervals..ts @@ -0,0 +1,32 @@ +class SummaryRanges { + numSet: Set; + + constructor() { + this.numSet = new Set(); + } + + addNum(value: number): void { + this.numSet.add(value); + } + + getIntervals(): number[][] { + let nums = Array.from(this.numSet.keys()); + nums.sort((a, b) => a - b); + let res: number[][] = []; + + let i = 0; + + while (i < nums.length) { + const start = nums[i]; + + while (i + 1 < nums.length && nums[i] + 1 == nums[i + 1]) { + i++; + } + + res.push([start, nums[i]]); + i++; + } + + return res; + } +}