栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

leetcode周赛290记录

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode周赛290记录

T3 统计包含每个点的矩形数目


  • 注意数据范围。纵坐标取值空间较小
  • 故在纵坐标维度上,对每个相同的纵坐标的横坐标进行排序。
  • 而不是反过来
class Solution {

public:
    vector countRectangles(vector>& rectangles, vector>& points) {
        vector df[101];
        for(vector a : rectangles)
        {
            df[a[1]].push_back(a[0]);
        }
        for(int i  =  0;i<101;i++) sort(df[i].begin(),df[i].end());
        vector count;
        for(vector p : points)
        {
            int tmp = 0;
            for(int i = p[1];i<101;i++)
            {
                tmp += df[i].end() - lower_bound(df[i].begin(),df[i].end(),p[0]);
            }
            count.push_back(tmp);
        }
        return count;
    }
};
T4花期内花的数目


  • 一来就想到线段树。给每个区间加上1.但是区间取值空间太大,线段树维护存不下
  • 特殊:区间个数较少,且每次区间都加的是1,而不是其他数。
  • 因此对给定的数字,搜索在其前面开花的区间,并搜索在其前面凋谢的区间。
class Solution {
public:
    vector fullBloomFlowers(vector>& flowers, vector& persons) {
        vector st;
        vector end;
        for(vector a:flowers)
        {
            st.push_back(a[0]);
            end.push_back(a[1]);
        }
        sort(st.begin(),st.end());
        sort(end.begin(),end.end());
        vector ans;
        for(int v : persons)
        {
            auto i = upper_bound(st.begin(),st.end(),v);
            auto j = upper_bound(end.begin(),end.end(),v);
            ans.push_back(i - st.begin()  - (j - end.begin()));
        }
        return ans;
    }
};
  • 另:比赛中反应出的C++问题
  • set迭代器不能用于计数
  • 自定义排序函数的写法可以是
        sort(rectangles.begin(), rectangles.end(), [](auto &a, auto &b) { return a[1] > b[1]; });

  • 注意变遍历为引用,可以节省复杂度:
    例如超时代码:
for(vector c : circles)
                {
                    int x1 = c[0];
                    int y1 = c[1];
                    int r = c[2];
                    if((x1 - x)*(x1 - x) + (y1 - y) * (y1 - y) <= r*r)
                    {
                        flag = true;
                        break;
                    }
                }

在vector之前加引用以后,不超时了:

class Solution {
public:
    int countLatticePoints(vector>& circles) {
        int ans = 0;
        for(int x = 0;x<=200;x++)
        {
            for(int  y = 0;y<=200;y++)
            {
                bool flag = false;
                for(vector& c : circles)
                {
                    int x1 = c[0];
                    int y1 = c[1];
                    int r = c[2];
                    if((x1 - x)*(x1 - x) + (y1 - y) * (y1 - y) <= r*r)
                    {
                        flag = true;
                        break;
                    }
                }
                ans += flag;
            }
        }
        return ans;
    }
};



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号