leetcode 周赛290
- 统计圆内格点数目
枚举坐标点,再枚举圆的数量,时间复杂度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:排序+二分
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞