|
| 1 | +// Source : https://leetcode.com/problems/minimum-interval-to-include-each-query/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-05-03 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] describes the i^th |
| 8 | + * interval starting at lefti and ending at righti (inclusive). The size of an interval is defined as |
| 9 | + * the number of integers it contains, or more formally righti - lefti + 1. |
| 10 | + * |
| 11 | + * You are also given an integer array queries. The answer to the j^th query is the size of the |
| 12 | + * smallest interval i such that lefti <= queries[j] <= righti. If no such interval exists, the answer |
| 13 | + * is -1. |
| 14 | + * |
| 15 | + * Return an array containing the answers to the queries. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5] |
| 20 | + * Output: [3,3,1,4] |
| 21 | + * Explanation: The queries are processed as follows: |
| 22 | + * - Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3. |
| 23 | + * - Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3. |
| 24 | + * - Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1. |
| 25 | + * - Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4. |
| 26 | + * |
| 27 | + * Example 2: |
| 28 | + * |
| 29 | + * Input: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22] |
| 30 | + * Output: [2,-1,4,6] |
| 31 | + * Explanation: The queries are processed as follows: |
| 32 | + * - Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2. |
| 33 | + * - Query = 19: None of the intervals contain 19. The answer is -1. |
| 34 | + * - Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4. |
| 35 | + * - Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + |
| 36 | + * 1 = 6. |
| 37 | + * |
| 38 | + * Constraints: |
| 39 | + * |
| 40 | + * 1 <= intervals.length <= 10^5 |
| 41 | + * 1 <= queries.length <= 10^5 |
| 42 | + * queries[i].length == 2 |
| 43 | + * 1 <= lefti <= righti <= 10^7 |
| 44 | + * 1 <= queries[j] <= 10^7 |
| 45 | + ******************************************************************************************************/ |
| 46 | + |
| 47 | +class Solution { |
| 48 | +public: |
| 49 | + vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) { |
| 50 | + set<vector<int>> s; |
| 51 | + |
| 52 | + vector<vector<int>> iQueries; |
| 53 | + for(int i=0; i < queries.size(); i++) { |
| 54 | + iQueries.push_back({queries[i], i}); |
| 55 | + } |
| 56 | + |
| 57 | + sort(intervals.begin(), intervals.end()); |
| 58 | + sort(iQueries.begin(), iQueries.end()); |
| 59 | + |
| 60 | + vector<int> result(queries.size(), -1); |
| 61 | + |
| 62 | + int i = 0, len = intervals.size(); |
| 63 | + |
| 64 | + for(auto& iq: iQueries) { |
| 65 | + int q = iq[0]; |
| 66 | + int idx = iq[1]; |
| 67 | + |
| 68 | + while( i < len && intervals[i][0] <= q) { |
| 69 | + s.insert({intervals[i][1] - intervals[i][0] + 1, intervals[i][1]}); |
| 70 | + i++; |
| 71 | + } |
| 72 | + while( !s.empty() ) { |
| 73 | + auto it = s.begin(); |
| 74 | + if ( (*it)[1] >= q ) break; |
| 75 | + s.erase(s.begin()); |
| 76 | + } |
| 77 | + if ( !s.empty() ) { |
| 78 | + auto it = s.begin(); |
| 79 | + result[idx] = (*it)[0]; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return result; |
| 84 | + } |
| 85 | +}; |
0 commit comments