leetcode 周赛290

  1. 统计圆内格点数目

枚举坐标点,再枚举圆的数量,时间复杂度200200200 8000000,但要注意C++ auto有拷贝耗时,用auto& 可以

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++) {
                for (auto& c : circles) {
                    if ((x - c[0]) * (x - c[0]) + (y - c[1]) * (y - c[1]) <= c[2] * c[2]) {
                        ans++;
                        break;
                    }
                }
            }
        }

        return ans;
    }
};

统计每个点的矩形
y的范围只有0到100,所以可以直接排序

class Solution {
public:
    vector<int> countRectangles(vector<vector<int>>& rectangles, vector<vector<int>>& points) {
        vector <int> ans;
        vector <vector<int>> G(105, vector<int>());

        for (auto& r : rectangles) {
            int x = r[0], y = r[1];
            G[y].push_back(x);
        }
        for (int y = 0; y <= 100; y++) {
            sort(G[y].begin(), G[y].end());
        }

        for (auto& p : points) {
            int x = p[0], cnt = 0;
            for (int y = p[1]; y <= 100; y++) {
                int size = G[y].end() - lower_bound(G[y].begin(), G[y].end(), x);
                cnt += size;
            }
            ans.push_back(cnt);
        }

        return ans;
    }
};

花开的花期

方法1:查分数组

class Solution {
public:
    vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& persons) {
        map <int, int> diff;
        for (auto& f : flowers) {
            diff[f[0]]++;
            diff[f[1] + 1]--;
        }

        int n = persons.size();
        vector<int> id(n, 0);
        sort(id.begin(), id.end(), [&](int i, int j) {
            return persons[i] < persons[j];
        });

        vector<int> ans(n);
        auto it = diff.begin();
        int sum = 0;
        for (int i : id) {
            while (it != diff.end() && it->first <= persons[i]) {
                sum += it->second;
                it++;
            }
            ans[i] = sum;
        }

        return ans;

    }
};

方法2:排序+二分

posted @   _西瓜不甜  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
点击右上角即可分享
微信分享提示