leetcode周赛290记录

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

在这里插入图片描述
在这里插入图片描述

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

public:
    vector<int> countRectangles(vector<vector<int>>& rectangles, vector<vector<int>>& points) {
        vector<int> df[101];
        for(vector<int> a : rectangles)
        {
            df[a[1]].push_back(a[0]);
        }
        for(int i  =  0;i<101;i++) sort(df[i].begin(),df[i].end());
        vector<int> count;
        for(vector<int> 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<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& persons) {
        vector<int> st;
        vector<int> end;
        for(vector<int> a:flowers)
        {
            st.push_back(a[0]);
            end.push_back(a[1]);
        }
        sort(st.begin(),st.end());
        sort(end.begin(),end.end());
        vector<int> 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<int> 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<vector<int>>& circles) {
        int ans = 0;
        for(int x = 0;x<=200;x++)
        {
            for(int  y = 0;y<=200;y++)
            {
                bool flag = false;
                for(vector<int>& 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;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值