Class Solution
java.lang.Object
g1801_1900.s1851_minimum_interval_to_include_each_query.Solution
1851 - Minimum Interval to Include Each Query.<p>Hard</p>
<p>You are given a 2D integer array <code>intervals</code>, where <code>intervals[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> interval starting at <code>left<sub>i</sub></code> and ending at <code>right<sub>i</sub></code> <strong>(inclusive)</strong>. The <strong>size</strong> of an interval is defined as the number of integers it contains, or more formally <code>right<sub>i</sub> - left<sub>i</sub> + 1</code>.</p>
<p>You are also given an integer array <code>queries</code>. The answer to the <code>j<sup>th</sup></code> query is the <strong>size of the smallest interval</strong> <code>i</code> such that <code>left<sub>i</sub> <= queries[j] <= right<sub>i</sub></code>. If no such interval exists, the answer is <code>-1</code>.</p>
<p>Return <em>an array containing the answers to the queries</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]</p>
<p><strong>Output:</strong> [3,3,1,4]</p>
<p><strong>Explanation:</strong> The queries are processed as follows:</p>
<ul>
<li>
<p>Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3.</p>
</li>
<li>
<p>Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3.</p>
</li>
<li>
<p>Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1.</p>
</li>
<li>
<p>Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.</p>
</li>
</ul>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]</p>
<p><strong>Output:</strong> [2,-1,4,6]</p>
<p><strong>Explanation:</strong> The queries are processed as follows:</p>
<ul>
<li>
<p>Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2.</p>
</li>
<li>
<p>Query = 19: None of the intervals contain 19. The answer is -1.</p>
</li>
<li>
<p>Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4.</p>
</li>
<li>
<p>Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 6.</p>
</li>
</ul>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= intervals.length <= 10<sup>5</sup></code></li>
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
<li><code>intervals[i].length == 2</code></li>
<li><code>1 <= left<sub>i</sub> <= right<sub>i</sub> <= 10<sup>7</sup></code></li>
<li><code>1 <= queries[j] <= 10<sup>7</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minInterval
public int[] minInterval(int[][] intervals, int[] queries)
-