Leetcode 第290场周赛

这篇博客主要介绍了LeetCode第290场周赛中的几道编程题,包括多个数组求交集、统计圆内格点数目、矩形与点的计数以及花期内花朵的数量。作者提供了详细的解题思路和代码实现,涉及数据结构与算法的应用,如字典、二分查找、差分等技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Leetcode 第290场周赛

这周是个菜狗
在这里插入图片描述

6041 多个数组求交集

class Solution:
    def intersection(self, nums: List[List[int]]) -> List[int]:
        dicts = defaultdict(int)
        n = len(nums)
        for num in nums:
            for items in num:
                dicts[items]+=1
        ans = []
        for item in dicts:
            if dicts[item]==n:
                ans.append(item)
        ans.sort()
        return ans

6042 统计圆内格点数目

判断那里没加while循环,一直没想到 掐点没交上 我菜哭了

class Solution {
public:
    int ans = 0;
    vector<vector<bool>> mp;
    void dfs(vector<int> v) {
        vector<int> row(2);
        int hight = v[1];
        row[0] = v[0]-v[2];
        row[1] = v[0]+v[2];
        while(hight<=v[1]+v[2]){
            while((hight-v[1]) * (hight-v[1]) + (row[1]-v[0])*(row[1]-v[0]) > v[2]*v[2]){
                row[0]+=1; row[1]-=1;
            }
            for(int i=row[0]; i<=row[1]; i++){
                if(!mp[i][hight]){
                    mp[i][hight] = true; ans+=1;
                }
            }
            hight +=1;
            
                
        }
        row[0] = v[0]-v[2]+1;
        row[1] = v[0]+v[2]-1;
        hight  = v[1] - 1;
        while(hight>=v[1]-v[2]){
            while((hight-v[1]) * (hight-v[1]) +(row[1]-v[0])*(row[1]-v[0]) > v[2]*v[2]){
                row[0]+=1; row[1]-=1;
            }
            for(int i=row[0]; i<=row[1]; i++){
                if(!mp[i][hight]){
                    mp[i][hight] = true; ans+=1;
                }
            }
            hight -=1;
        }

    }
    int countLatticePoints(vector<vector<int>>& circles) {
        int n = circles.size();
        mp.resize(1000,vector<bool>(1000,false));
        for(int i=0;i<n;i++)    dfs(circles[i]);
        return ans;
    }
};

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

参考思路1:y最大100,rectangles的x放到0-100容器中,然后二分遍历找在points y>=y-100中找比point x大的加起来。
参考思路2:将rectangle 的x从大到小排序,然后points 记录id 从大到小排序,用个有序的set维护和记录对y排序,47 / 47 个通过测试用例 状态:超出时间限制

//思路1 打败100%
vector<int> countRectangles(vector<vector<int>>& rectangles, vector<vector<int>>& points) {
    int n = points.size(), m = rectangles.size();
    vector<vector<int>> buttom(100+1);
    vector<int> ans(n,0);
    for(int i=0;i<m;i++) 
        buttom[rectangles[i][1]].push_back(rectangles[i][0]);
    for(int i=0;i<=100;i++) 
        sort(buttom[i].begin(),buttom[i].end());//从大到小
    for(int i=0;i<n;i++) {
        for(int j=points[i][1];j<=100;j++){
            if(buttom[j].size()==0) continue;
            int ix = lower_bound(buttom[j].begin(),buttom[j].end(),points[i][0])-buttom[j].begin();
            ans[i] += buttom[j].size()-ix;
        }
    } 
    return ans;
}
class Solution {
public:
    vector<int> countRectangles(vector<vector<int>>& rectangles, vector<vector<int>>& points) {
        int n = points.size(), m = rectangles.size();
        vector<vector<int>> nPoint(n,vector<int>(3));
        for(int i=0;i<n;i++) {
            nPoint[i][0]=points[i][0];
            nPoint[i][1]=points[i][1];
            nPoint[i][2]=i;
        }
        // 横的排序 从大到小
        sort(rectangles.begin(),rectangles.end(),[](vector<int> a,vector<int> b){return a[0]>b[0];});
        sort(nPoint.begin(),nPoint.end(),[](vector<int> a,vector<int> b){return a[0]>b[0];});
        vector<int> ans(n,0);
        vector<int> count(100+1,0);
        set<int> s;
        int ix = 0, id;
        for(int i=0;i<n;i++) {
            while(ix<m && rectangles[ix][0]>=nPoint[i][0]){ //放入满足>x的条件的 y 
                s.insert(rectangles[ix][1]);
                count[rectangles[ix][1]]+=1;
                ix++;
            }
            auto it = s.lower_bound(nPoint[i][1]);
            while(it!=s.end()){
                ans[nPoint[i][2]]+= count[*it]; it++;
            }
        }
        return ans;
    }
};

6044 花期内花的数目

思路:差分

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& persons) {
        map<int,int> diff;
        for(auto &flower: flowers) {
            diff[flower[0]]++;
            diff[flower[1]+1]--;
        }
        int n = persons.size();
        vector<int> ans(n),id(n);
        iota(id.begin(),id.end(),0);
        sort(id.begin(),id.end(),[&](int i,int j){
            return persons[i]<persons[j];
        });
        int sum = 0;
        auto it = diff.begin();
        for(auto &i: id) {
            while(it!=diff.end() && it->first<=persons[i]){
                sum+=it->second;
                it++;
            }
            ans[i] = sum;
        }
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值