Skip to content

Commit d8731d0

Browse files
committed
New Problem Solution - "1851. Minimum Interval to Include Each Query"
1 parent fb308ad commit d8731d0

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1851|[Minimum Interval to Include Each Query](https://leetcode.com/problems/minimum-interval-to-include-each-query/) | [C++](./algorithms/cpp/minimumIntervalToIncludeEachQuery/MinimumIntervalToIncludeEachQuery.cpp)|Hard|
1213
|1850|[Minimum Adjacent Swaps to Reach the Kth Smallest Number](https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/) | [C++](./algorithms/cpp/minimumAdjacentSwapsToReachTheKthSmallestNumber/MinimumAdjacentSwapsToReachTheKthSmallestNumber.cpp)|Medium|
1314
|1849|[Splitting a String Into Descending Consecutive Values](https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/) | [C++](./algorithms/cpp/splittingAStringIntoDescendingConsecutiveValues/SplittingAStringIntoDescendingConsecutiveValues.cpp)|Medium|
1415
|1848|[Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element/) | [C++](./algorithms/cpp/minimumDistanceToTheTargetElement/MinimumDistanceToTheTargetElement.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)