Title: #715 - Range Module - LeetCode JavaScript Solutions
Open Graph Title: #715 - Range Module - LeetCode JavaScript Solutions
X Title: #715 - Range Module - LeetCode JavaScript Solutions
Description: A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as half-open intervals and...
Open Graph Description: A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as half-open intervals and...
X Description: A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as half-open intervals and...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/range-module
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/range-module","name":"#715 - Range Module - LeetCode JavaScript Solutions","description":"A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as half-open intervals and...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"var RangeModule = function() {\n this.ranges = [];\n};\n\n/**\n * @param {number} left\n * @param {number} right\n * @return {void}\n */\nRangeModule.prototype.addRange = function(left, right) {\n const intervals = [];\n let placed = false;\n for (const [start, end] of this.ranges) {\n if (start > right && !placed) {\n intervals.push([left, right]);\n placed = true;\n }\n if (end < left || start > right) {\n intervals.push([start, end]);\n } else {\n left = Math.min(left, start);\n right = Math.max(right, end);\n }\n }\n if (!placed) intervals.push([left, right]);\n this.ranges = intervals;\n};\n\n/**\n * @param {number} left\n * @param {number} right\n * @return {boolean}\n */\nRangeModule.prototype.queryRange = function(left, right) {\n for (const [start, end] of this.ranges) {\n if (start <= left && end >= right) return true;\n if (start > left) break;\n }\n return false;\n};\n\n/**\n * @param {number} left\n * @param {number} right\n * @return {void}\n */\nRangeModule.prototype.removeRange = function(left, right) {\n const intervals = [];\n for (const [start, end] of this.ranges) {\n if (end <= left || start >= right) {\n intervals.push([start, end]);\n } else {\n if (start < left) intervals.push([start, left]);\n if (end > right) intervals.push([right, end]);\n }\n }\n this.ranges = intervals;\n};","keywords":"LeetCode 715, #715 - Range Module, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/range-module |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/range-module/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0715-range-module.js |
| Design | https://leetcodejavascript.com/tags/design |
| Ordered Set | https://leetcodejavascript.com/tags/ordered-set |
| Segment Tree | https://leetcodejavascript.com/tags/segment-tree |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow