[One question per day] 1851. Contains the minimum interval of each query

[One question per day] 1851. Contains the minimum interval of each query

1851. Include Minimum Intervals for Every Query

topic description

You are given a two-dimensional integer array intervals, where intervals[i] = [lefti, righti] means that the i-th interval starts at lefti and ends at righti (including values ​​on both sides, closed interval). The length of an interval is defined as the number of integers contained in the interval, more formally expressed as righti - lefti + 1.

Then you are given an integer array queries. The answer to the jth query is the length of the minimum interval i that satisfies lefti <= queries[j] <= righti. If no such interval exists, then the answer is -1.

Returns all answers for the corresponding query as an array.

Example 1:

输入:intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
输出:[3,3,1,4]
解释:查询处理如下:
- Query = 2 :区间 [2,4] 是包含 2 的最小区间,答案为 4 - 2 + 1 = 3 。
- Query = 3 :区间 [2,4] 是包含 3 的最小区间,答案为 4 - 2 + 1 = 3 。
- Query = 4 :区间 [4,4] 是包含 4 的最小区间,答案为 4 - 4 + 1 = 1 。
- Query = 5 :区间 [3,6] 是包含 5 的最小区间,答案为 6 - 3 + 1 = 4 。

Example 2:

输入:intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
输出:[2,-1,4,6]
解释:查询处理如下:
- Query = 2 :区间 [2,3] 是包含 2 的最小区间,答案为 3 - 2 + 1 = 2 。
- Query = 19:不存在包含 19 的区间,答案为 -1 。
- Query = 5 :区间 [2,5] 是包含 5 的最小区间,答案为 5 - 2 + 1 = 4 。
- Query = 22:区间 [20,25] 是包含 22 的最小区间,答案为 25 - 20 + 1 = 6 。

hint:

1 <= intervals.length <= 105
1 <= queries.length <= 105
intervals[i].length == 2
1 <= lefti <= righti <= 107
1 <= queries[j] <= 107

problem solving ideas

Idea: sorting + offline query + priority queue. Through observation, we can find that the order of the query and the order of the intervals will not affect the results, so we can sort the queries and intervals first. Since the final returned results must follow the original query order, we need to save their subscripts when sorting the queries. For each query, traverse the interval with the smallest length to which it belongs, then it needs to satisfy l<=q<=r, which is equivalent to two-dimensional sorting. We cannot process two dimensions at the same time, so we can process l first and then r, that is, first collect the ones that satisfy l<=q, and then remove the part of q>r from it, and finally the smallest interval length is the result. For part l, we can collect l<=q after sorting the intervals according to the left endpoint, and then we require the minimum interval, we can use a small root heap to store these collected l<=q, and then exclude the part of q>r from it. At this time, r is used, so the minimum heap can store the length of the interval and the right endpoint, which is [len,r].

class Solution {
public:
    vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) {
        // 查询顺序不会影响答案 涉及区间也不会变化
        int n=intervals.size();
        int m=queries.size();
        // 区间按照左端点从小到大排序
        // 对区间和查询值都进行排序。排序的目的是我们可以过滤掉非备选区间:即lefti > queries[j]和righti < queries[j]
        sort(intervals.begin(),intervals.end());
        // 定义别名
        using pii=pair<int,int>;
        //查询以及原始下标
        vector<pii> qs;
        for(int i=0;i<m;i++)
            qs.emplace_back(queries[i],i);
        // 查询按照从小到大排序
        sort(qs.begin(),qs.end());
        vector<int> ans(m,-1);
        // 就和int vector<int> greater<int>一样  greater是小根堆
        // [v,r]表示区间长度 右端点
        priority_queue<pii,vector<pii>,greater<pii>> pq;
        int i=0;
        //遍历查询
        for(auto &[x,j]:qs)
        {
            //当前遍历区间   过滤掉lefti > queries[j]
            while(i<n&&intervals[i][0]<=x)
            {
                int a=intervals[i][0],b=intervals[i][1];
                //如果区间左端点小于当前查询数则加入
                pq.emplace(b-a+1,b);
                i++;
            } 
            //将右端点小于的删除    过滤掉righti < queries[j]
            while(!pq.empty()&&pq.top().second<x)
                pq.pop();
            //最后加入满足要求的
            if(!pq.empty())
                ans[j]=pq.top().first;
        }
        return ans;
    }
};

Summary: Use the sort function for a two-dimensional array, which will be sorted according to the first dimension. priority_queue<int, vector<int>, greater<int>> is a small root heap, and int can be replaced with other data types. using pii = vector<int,int> is equivalent to using an alias.

Guess you like

Origin blog.csdn.net/qq_43779149/article/details/131796075