forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2163 from AkifhanIlgaz/0352
Create: 0352-data-stream-as-disjoint-intervals
- Loading branch information
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// BTreeSet Solution | ||
|
||
use std::collections::BTreeSet; | ||
|
||
struct SummaryRanges { | ||
tree_map: BTreeSet<i32>, | ||
} | ||
|
||
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<Vec<i32>> { | ||
let mut res: Vec<Vec<i32>> = 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<Vec<i32>>, | ||
num_set: HashSet<i32>, | ||
} | ||
|
||
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<Vec<i32>> { | ||
let mut nums: Vec<i32> = 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class SummaryRanges { | ||
numSet: Set<number>; | ||
|
||
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; | ||
} | ||
} |