Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d9b609c

Browse files
committed
Add solution #715
1 parent 878ebd0 commit d9b609c

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

0715-range-module.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* 715. Range Module
3+
* https://leetcode.com/problems/range-module/
4+
* Difficulty: Hard
5+
*
6+
* A Range Module is a module that tracks ranges of numbers. Design a data structure to track the
7+
* ranges represented as half-open intervals and query about them.
8+
*
9+
* A half-open interval [left, right) denotes all the real numbers x where left <= x < right.
10+
*
11+
* Implement the RangeModule class:
12+
* - RangeModule() Initializes the object of the data structure.
13+
* - void addRange(int left, int right) Adds the half-open interval [left, right), tracking every
14+
* real number in that interval. Adding an interval that partially overlaps with currently tracked
15+
* numbers should add any numbers in the interval [left, right) that are not already tracked.
16+
* - boolean queryRange(int left, int right) Returns true if every real number in the interval
17+
* [left, right) is currently being tracked, and false otherwise.
18+
* - void removeRange(int left, int right) Stops tracking every real number currently being tracked
19+
* in the half-open interval [left, right).
20+
*/
21+
22+
var RangeModule = function() {
23+
this.ranges = [];
24+
};
25+
26+
/**
27+
* @param {number} left
28+
* @param {number} right
29+
* @return {void}
30+
*/
31+
RangeModule.prototype.addRange = function(left, right) {
32+
const intervals = [];
33+
let placed = false;
34+
for (const [start, end] of this.ranges) {
35+
if (start > right && !placed) {
36+
intervals.push([left, right]);
37+
placed = true;
38+
}
39+
if (end < left || start > right) {
40+
intervals.push([start, end]);
41+
} else {
42+
left = Math.min(left, start);
43+
right = Math.max(right, end);
44+
}
45+
}
46+
if (!placed) intervals.push([left, right]);
47+
this.ranges = intervals;
48+
};
49+
50+
/**
51+
* @param {number} left
52+
* @param {number} right
53+
* @return {boolean}
54+
*/
55+
RangeModule.prototype.queryRange = function(left, right) {
56+
for (const [start, end] of this.ranges) {
57+
if (start <= left && end >= right) return true;
58+
if (start > left) break;
59+
}
60+
return false;
61+
};
62+
63+
/**
64+
* @param {number} left
65+
* @param {number} right
66+
* @return {void}
67+
*/
68+
RangeModule.prototype.removeRange = function(left, right) {
69+
const intervals = [];
70+
for (const [start, end] of this.ranges) {
71+
if (end <= left || start >= right) {
72+
intervals.push([start, end]);
73+
} else {
74+
if (start < left) intervals.push([start, left]);
75+
if (end > right) intervals.push([right, end]);
76+
}
77+
}
78+
this.ranges = intervals;
79+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@
540540
712|[Minimum ASCII Delete Sum for Two Strings](./0712-minimum-ascii-delete-sum-for-two-strings.js)|Medium|
541541
713|[Subarray Product Less Than K](./0713-subarray-product-less-than-k.js)|Medium|
542542
714|[Best Time to Buy and Sell Stock with Transaction Fee](./0714-best-time-to-buy-and-sell-stock-with-transaction-fee.js)|Medium|
543+
715|[Range Module](./0715-range-module.js)|Hard|
543544
717|[1-bit and 2-bit Characters](./0717-1-bit-and-2-bit-characters.js)|Easy|
544545
720|[Longest Word in Dictionary](./0720-longest-word-in-dictionary.js)|Medium|
545546
722|[Remove Comments](./0722-remove-comments.js)|Medium|

0 commit comments

Comments
 (0)