2940. 找到 Alice 和 Bob 可以相遇的建筑
题目链接:2940. 找到 Alice 和 Bob 可以相遇的建筑
代码如下:
cpp
//参考链接:https://leetcode.cn/problems/find-building-where-alice-and-bob-can-meet/solutions/2533058/chi-xian-zui-xiao-dui-pythonjavacgo-by-e-9ewj
class Solution
{
public:
vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries)
{
vector<int> res(queries.size(),-1);
vector<vector<pair<int,int>>> qs(heights.size());
for(int i=0;i<queries.size();i++)
{
int a=queries[i][0],b=queries[i][1];
if(a>b)
{
swap(a,b);
}
if(a==b||heights[a]<heights[b])
{
res[i]=b;
}
else
{
qs[b].emplace_back(heights[a],i);
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> pq;
for(int i=0;i<heights.size();i++)
{
while(!pq.empty()&&pq.top().first<heights[i])
{
res[pq.top().second]=i;
pq.pop();
}
for(auto&p:qs[i])
{
pq.emplace(p);
}
}
return res;
}
};