当前位置: 首页 > news >正文

map|math

 

 

lcr34

map<char,int> 处理order

tmp=word

按order sort lambda tmp;

return tmp==word;

class Solution {

    unordered_map<char, int> hash;

public:

    bool isAlienSorted(vector<string>& words, string order) {

        int n = order.size();

        for (int i = 0; i < n; i++) {

            hash[order[i]] = i;

        }

 

        vector<string> corr = words;

        // 自定义排序规则,按外星语字典序排序

        sort(corr.begin(), corr.end(), [&](const string& a, const string& b) {

            int lenA = a.size(), lenB = b.size();

            int minLen = min(lenA, lenB);

            for (int i = 0; i < minLen; i++) {

                // 逐个字符比较,按外星语顺序判断

                if (hash[a[i]] != hash[b[i]]) {

                    return hash[a[i]] < hash[b[i]];

                }

            }

            // 前面字符都相同,短单词更小

            return lenA < lenB;

        });

        return corr == words;

    }

};

 

lcp42.玩具套圈 abstract

先给玩具“按r 分门别类”map<int, set<int>> A[12],再逐个圆圈排查能装下哪些玩具,最后算总数

统计能被圆圈完全覆盖的玩具数量

1. 整理玩具信息
用一个特殊结构 A 存储玩具:

按玩具的半径 r 分类( A[r] ),同一半径的玩具再按x坐标分组( A[r][x] ),每组里用集合存对应的y坐标。这样方便后续快速查找特定半径和x范围内的玩具。


2. 检查每个圆圈能覆盖的玩具
对每个圆圈,遍历所有可能被它覆盖的玩具半径 r (从1到圆圈半径 R ,因为玩具半径必须≤圆圈半径才可能被覆盖)。
每个半径 r ,计算该圆圈能覆盖的x坐标范围,找到这个范围内的玩具x坐标,再进一步计算对应的y坐标范围。
最后在符合条件的x组里,找出y坐标在范围内的玩具,标记它们被覆盖(从集合中删除,避免重复统计),并计数。


3. 返回总覆盖数
所有圆圈检查完后,累计的被覆盖玩具数量就是结果。

 

class Solution

{
map<int, set<int>> A[12];

public:
int circleGame(vector<vector<int>>& toys, vector<vector<int>>& circles, int R)

{
int n = toys.size();
for (const vector<int> &vec : toys) A[vec[2]][vec[0]].insert(vec[1]);

 

        int ans = 0;
for (const vector<int> &vec : circles)

       {
for (int r = 1; r <= R; r++)

          for (int x = vec[0] - R + r; x <= vec[0] + R - r; x++)  //x范围

           if (A[r].count(x))

        {
int a = abs(vec[0] - x);
int b = (int) sqrt((R - r) * (R - r) - a * a + 1e-9);
int y1 = vec[1] - b, y2 = vec[1] + b;

 

                set<int> &st = A[r][x];

//遍历set查找
for (auto it = st.lower_bound(y1); it != st.end() && *it <= y2; )

               {
auto nxt = next(it);
st.erase(it);
ans++;
it = nxt;
}
}
}
return ans;
}
};

 

 

lc42

dp找每个元素的maxl和maxr,ret+=min

class Solution {
public:
int trap(vector<int>& height) {
int totalWater = 0;
int left = 0;
int right = height.size() - 1;
int leftMax = 0;
int rightMax = 0;

while (left < right) {
leftMax = max(leftMax, height[left]);
rightMax = max(rightMax, height[right]);

if (leftMax < rightMax) {
totalWater += leftMax - height[left];
left++;
} else {
totalWater += rightMax - height[right];
right--;
}
}

return totalWater;
}
};

 

http://www.xdnf.cn/news/1375003.html

相关文章:

  • 腾讯位置商业授权微信小程序路线规划
  • 【开源工具】基于Flask与Socket.IO的跨平台屏幕监控系统实战(附完整源码)
  • 前端性能优化:从指标监控到全链路落地(2024最新实战指南)
  • 论文阅读:Gorilla: Large Language Model Connected with Massive APIs
  • 深度学习入门:神经网络基础知识
  • lesson47:Linux常用软件使用指南:远程连接、远程拷贝、Vim与Nginx
  • VESA时序检测模块设计verilog实现
  • Ubuntu 24 Server 如何设置无线网络
  • imx6ull-驱动开发篇45——Linux 下 SPI 驱动框架简介
  • d435i相机读取镜头内参和相对之间的外参
  • 艾体宝新闻 | 98%好评率!KnowBe4 连续5年蝉联第一,现开放免费钓鱼测试等你解锁
  • 内网应用如何实现外网访问?外地通过公网地址访问内网服务器的设置方法
  • 遗传算法:模拟自然选择的优化智慧
  • Spring Boot项目集成日志系统使用完整指南
  • 欧洲数字化养殖平台 Herdwatch 借力 Iceberg + StarRocks 提升分析能力
  • 嵌入式开发学习 C++:day01
  • 动态规划:硬币兑换(有趣)
  • LeetCode - 739. 每日温度
  • 线性回归原理推导与应用(十一):多重共线性
  • 获取服务器指标的信息
  • bin log 和 redo log有什么区别
  • Mybatis总结
  • 【如何解决Java中的ClassCastException类转换异常问题】
  • 基于Matlab结合肤色检测与卷积神经网络的人脸识别方法研究
  • 基于MATLAB/Simulink的单机带负荷仿真系统搭建
  • 分布式2PC理论
  • 使用 html2canvas + jspdf 实现页面元素下载为pdf文件
  • UE5 查找组件
  • 云原生安全架构设计与零信任实践
  • 预测模型及超参数:1.传统机器学习:SVR与KNN