Class RangeModule

java.lang.Object
g0701_0800.s0715_range_module.RangeModule

public class RangeModule extends Object
715 - Range Module.<p>Hard</p> <p>A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as <strong>half-open intervals</strong> and query about them.</p> <p>A <strong>half-open interval</strong> <code>[left, right)</code> denotes all the real numbers <code>x</code> where <code>left <= x < right</code>.</p> <p>Implement the <code>RangeModule</code> class:</p> <ul> <li><code>RangeModule()</code> Initializes the object of the data structure.</li> <li><code>void addRange(int left, int right)</code> Adds the <strong>half-open interval</strong> <code>[left, right)</code>, tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval <code>[left, right)</code> that are not already tracked.</li> <li><code>boolean queryRange(int left, int right)</code> Returns <code>true</code> if every real number in the interval <code>[left, right)</code> is currently being tracked, and <code>false</code> otherwise.</li> <li><code>void removeRange(int left, int right)</code> Stops tracking every real number currently being tracked in the <strong>half-open interval</strong> <code>[left, right)</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;RangeModule&rdquo;, &ldquo;addRange&rdquo;, &ldquo;removeRange&rdquo;, &ldquo;queryRange&rdquo;, &ldquo;queryRange&rdquo;, &ldquo;queryRange&rdquo;]</p> <p>[ [], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]</p> <p><strong>Output:</strong> [null, null, null, true, false, true]</p> <p><strong>Explanation:</strong></p> <pre><code> RangeModule rangeModule = new RangeModule(); rangeModule.addRange(10, 20); rangeModule.removeRange(14, 16); rangeModule.queryRange(10, 14); // return True,(Every number in [10, 14) is being tracked) rangeModule.queryRange(13, 15); // return False,(Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked) rangeModule.queryRange(16, 17); // return True, (The number 16 in [16, 17) is still being tracked, despite the remove operation) </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= left < right <= 10<sup>9</sup></code></li> <li>At most <code>10<sup>4</sup></code> calls will be made to <code>addRange</code>, <code>queryRange</code>, and <code>removeRange</code>.</li> </ul>
  • Constructor Details

    • RangeModule

      public RangeModule()
  • Method Details

    • addRange

      public void addRange(int left, int right)
    • queryRange

      public boolean queryRange(int left, int right)
    • removeRange

      public void removeRange(int left, int right)