对std::set使用lower_bound的效率问题

在解决1488题洪水泛滥时,使用std::set的lower_bound方法出现性能问题。通过对比,发现直接调用lower_bound函数与set实例的方法存在效率差异,后者导致超时。分析指出,可能的原因是std::set内部实现导致的查找效率不同。
摘要由CSDN通过智能技术生成

1488题,洪水泛滥。在查找可用晴天时,如果使用 auto last_sunny_it = sunny.lower_bound(last_rain);就会超时。

而如果使用auto last_sunny_it = lower_bound(sunny.begin(),sunny.end(),last_rain); 就可以通过,性能较好。这2个lower_bound有什么差别吗?

vector<int> avoidFlood(vector<int>& rains) {
        int n = rains.size();
        vector<int> ans(n,1);
        unordered_map<int,int> pool2last_rain;
        set<int> sunny;
        for(int idx = 0;idx < n;idx++){
            if(rains[idx] == 0){
                sunny.insert(idx);
                continue;
            }
            if(pool2last_rain.count(rains[idx]) != 0){
                int last_rain = pool2last_rain[rains[idx]];
                auto last_sunny_it = lower_bound(sunny.begin(),sunny.end(),last_rain);
                //auto last_sunny_it = sunny.lower_bound(last_rain);
                if(last_sunny_it == sunny.end(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值