2940. 找到 Alice 和 Bob 可以相遇的建筑

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;
    }
};
相关推荐
攻城狮7号10 分钟前
【第五节】C++设计模式(创建型模式)-Prototype(原型)模式
c++·设计模式·原型模式
请卧龙先生出山12 分钟前
c++day4
开发语言·c++
愚戏师15 分钟前
从零到一学习c++(基础篇--筑基期十一-类)
开发语言·数据结构·c++·学习·算法
轩源源1 小时前
unordered_set和unordered_map的使用
开发语言·数据结构·c++·算法·哈希算法·unordered_map·unordered_set
挨代码5 小时前
UE_C++ —— Gameplay Modules
c++·ue
攻城狮7号10 小时前
【第四节】C++设计模式(创建型模式)-Builder(建造者)模式
c++·设计模式·建造者模式
fpcc10 小时前
设计心得——解耦的实现技术
c++·软件工程
东方芷兰10 小时前
算法笔记 04 —— 算法初步(下)
c++·笔记·算法
xinghuitunan11 小时前
时间转换(acwing)c/c++/java/python
java·c语言·c++·python
TechNomad11 小时前
C++访问MySQL数据库
数据库·c++·mysql