当前位置:网站首页>452. 用最少数量的箭引爆气球
452. 用最少数量的箭引爆气球
2022-07-23 09:22:00 【empty__barrel】
subject:
有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points ,其中points[i] = [xstart, xend] 表示水平直径在 xstart 和 xend之间的气球。你不知道气球的确切 y 坐标。
一支弓箭可以沿着 x 轴从不同点 完全垂直 地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被 引爆 。可以射出的弓箭的数量 没有限制 。 弓箭一旦被射出之后,可以无限地前进。
给你一个数组 points ,返回引爆所有气球所必须射出的 最小 弓箭数 。
仔细分析还是很巧妙,都很巧妙,我很无语,就是要想半天。
垃圾解法:
我的初始思想是将题目分为了两步:
1 . 每一个都会消灭
2 . 有耦合度的一起消灭
依次遍历每一个元素,记录每组起始元素范围尾值,
首先是第一个元素,我记录它的范围尾值,然后依次遍历后面的元素直到有元素的范围头值大于之前记录的范围尾值此时就sum++。想法很美好,现实很残酷。
首先解决在遍历第一个元素时它没有前一个值怎么办,OK,此时我思考了会,就把他自己当做pre,然后自己跟自己比较也没事,OK。然后出现了范围头值大于之前记录的范围尾值此时进行sum++,OK,这个又是头元素那么还要进行跟第一个元素一样的操作,OK。这种类型的思考过了,开始下一步,那么假设此时到达了最后的元素。
分情况了
1:没有最后元素,怎么办,怎么++
2:有最后元素,但是只有这一个元素怎么办,即等同于1,但是又不同。,不同在于此时更新了now,和pre两个边界尾值。
此时我使用了双变量来记录pre和now即两个边界尾值。最后一个元素边界头跟now边界尾值比较解决第一个问题,now边界尾跟pre边界尾进行比较解决第二个问题。很NICE
“巧妙”解法
思想不变
1.全部都会消灭
2.有耦合度的消灭
所以将其从左往右按照耦合度分为n组(当后面元素的范围与前面组首元素的范围没有耦合度那么这个元素就成了下一组的首元素)即可,当遍历到每一组的第一个就sum++且更新判断条件now = elem【1】即可,此时第一个元素没有前面的元素怎么办呢,直接++即可,岂不快哉。办法挺好,就是费脑。
没看题解自己想很舒服的,提高很快的(狗头),希望你们都这样!
代码如下:
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if (points.empty()) {
return 0;
}
sort(points.begin(), points.end(), [](const vector<int>& u, const vector<int>& v) {
return u[1] < v[1];
});
int now= points[0][1];
int ans = 1;
for (auto elem: points) {
if (elem[0] > now) {
now= elem[1];
++ans;
}
}
return ans;
}
};
边栏推荐
- Towhee 每周模型
- Official wechat product! Applet automation framework minium sharing Preview
- CSDN写文方法(二)
- 什麼是Per-Title編碼?
- 转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
- Authing 支持 Zadig 啦!云原生用户统一认证快速对接
- Consensys Quorum Benchmark Test
- Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated
- Okrk3399 Development Board reserves i2c4 to mount EEPROM
- uni-app知识点和项目上遇到的问题和解决办法的记录
猜你喜欢
![[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning](/img/3d/6486f836535e5a1fa1a362d5214d77.png)
[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning

关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论

C language implementation of classroom random roll call system

AI acceleration gesture recognition experience based on efr32mg24

Quick introduction to PKI system

运维高级作业02

利用js自动解析执行xss

转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
![Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)](/img/f0/9491ccc2a86d95bb30066397fb9fb6.png)
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)

Consensys Quorum Benchmark Test
随机推荐
Authing 支持 Zadig 啦!云原生用户统一认证快速对接
C language implements StrCmp, strstr, strcat, strcpy
Chapter 2 basic query and sorting
运维高级作业03
炫酷代码雨动态背景注册页面
Game (2) of 2022 Henan Mengxin League: solution to supplementary questions of Henan University of Technology
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
Aruba学习笔记05-配置架构- WLAN配置架构
可以进行2D或3D三角剖分的一些库
js纹理样式饼图插件
Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
js日历样式饼图统计插件
优化华为云服务器采用Key登陆
Question 142 of Li Kou: circular linked list 2
Cool code rain dynamic background registration page
基于nextcloud构建个人网盘
Okrk3399 Development Board reserves i2c4 to mount EEPROM
Authing supports Zadig! Unified authentication and rapid docking of cloud native users
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)