|
| 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 | +}; |
0 commit comments