Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2163 from AkifhanIlgaz/0352
Browse files Browse the repository at this point in the history
Create: 0352-data-stream-as-disjoint-intervals
  • Loading branch information
tahsintunan authored Jan 30, 2023
2 parents 4f1101e + d381471 commit 0c16a50
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
44 changes: 44 additions & 0 deletions go/0352-data-stream-as-disjoint-intervals.go
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() {

}
31 changes: 31 additions & 0 deletions javascript/0352-data-stream-as-disjoint-intervals.js
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;
}
}
73 changes: 73 additions & 0 deletions rust/0352-data-stream-as-disjoint-intervals..rs
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
}
}
32 changes: 32 additions & 0 deletions typescript/0352-data-stream-as-disjoint-intervals..ts
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;
}
}

0 comments on commit 0c16a50

Please sign in to comment.