Skip to content

Commit 91721d6

Browse files
committed
feat: add typescript solution to lc problem: No.2276
No.2276.Count Integers in Intervals
1 parent ef8eb3d commit 91721d6

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

solution/2200-2299/2276.Count Integers in Intervals/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,45 @@ class CountIntervals {
240240
### **TypeScript**
241241

242242
```ts
243+
class CountIntervals {
244+
left: null | CountIntervals;
245+
right: null | CountIntervals;
246+
start: number;
247+
end: number;
248+
sum: number;
249+
constructor(start: number = 0, end: number = 10 ** 9) {
250+
this.left = null;
251+
this.right = null;
252+
this.start = start;
253+
this.end = end;
254+
this.sum = 0;
255+
}
256+
257+
add(left: number, right: number): void {
258+
if (this.sum == (this.end - this.start + 1)) return;
259+
if (left <= this.start && right >= this.end) {
260+
this.sum = this.end - this.start + 1;
261+
return;
262+
}
263+
let mid = (this.start + this.end) >> 1;
264+
if (!this.left) this.left = new CountIntervals(this.start, mid);
265+
if (!this.right) this.right = new CountIntervals(mid + 1, this.end);
266+
if (left <= mid) this.left.add(left, right);
267+
if (right > mid) this.right.add(left, right);
268+
this.sum = this.left.sum + this.right.sum;
269+
}
243270

271+
count(): number {
272+
return this.sum;
273+
}
274+
}
275+
276+
/**
277+
* Your CountIntervals object will be instantiated and called as such:
278+
* var obj = new CountIntervals()
279+
* obj.add(left,right)
280+
* var param_2 = obj.count()
281+
*/
244282
```
245283

246284
### **...**

solution/2200-2299/2276.Count Integers in Intervals/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,45 @@ class CountIntervals {
227227
### **TypeScript**
228228

229229
```ts
230+
class CountIntervals {
231+
left: null | CountIntervals;
232+
right: null | CountIntervals;
233+
start: number;
234+
end: number;
235+
sum: number;
236+
constructor(start: number = 0, end: number = 10 ** 9) {
237+
this.left = null;
238+
this.right = null;
239+
this.start = start;
240+
this.end = end;
241+
this.sum = 0;
242+
}
243+
244+
add(left: number, right: number): void {
245+
if (this.sum == (this.end - this.start + 1)) return;
246+
if (left <= this.start && right >= this.end) {
247+
this.sum = this.end - this.start + 1;
248+
return;
249+
}
250+
let mid = (this.start + this.end) >> 1;
251+
if (!this.left) this.left = new CountIntervals(this.start, mid);
252+
if (!this.right) this.right = new CountIntervals(mid + 1, this.end);
253+
if (left <= mid) this.left.add(left, right);
254+
if (right > mid) this.right.add(left, right);
255+
this.sum = this.left.sum + this.right.sum;
256+
}
230257

258+
count(): number {
259+
return this.sum;
260+
}
261+
}
262+
263+
/**
264+
* Your CountIntervals object will be instantiated and called as such:
265+
* var obj = new CountIntervals()
266+
* obj.add(left,right)
267+
* var param_2 = obj.count()
268+
*/
231269
```
232270

233271
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class CountIntervals {
2+
left: null | CountIntervals;
3+
right: null | CountIntervals;
4+
start: number;
5+
end: number;
6+
sum: number;
7+
constructor(start: number = 0, end: number = 10 ** 9) {
8+
this.left = null;
9+
this.right = null;
10+
this.start = start;
11+
this.end = end;
12+
this.sum = 0;
13+
}
14+
15+
add(left: number, right: number): void {
16+
if (this.sum == (this.end - this.start + 1)) return;
17+
if (left <= this.start && right >= this.end) {
18+
this.sum = this.end - this.start + 1;
19+
return;
20+
}
21+
let mid = (this.start + this.end) >> 1;
22+
if (!this.left) this.left = new CountIntervals(this.start, mid);
23+
if (!this.right) this.right = new CountIntervals(mid + 1, this.end);
24+
if (left <= mid) this.left.add(left, right);
25+
if (right > mid) this.right.add(left, right);
26+
this.sum = this.left.sum + this.right.sum;
27+
}
28+
29+
count(): number {
30+
return this.sum;
31+
}
32+
}
33+
34+
/**
35+
* Your CountIntervals object will be instantiated and called as such:
36+
* var obj = new CountIntervals()
37+
* obj.add(left,right)
38+
* var param_2 = obj.count()
39+
*/

0 commit comments

Comments
 (0)