Skip to content

Commit f0bfbd9

Browse files
committed
feat: add minimum number of arrows to burst balloons
1 parent a96a893 commit f0bfbd9

File tree

5 files changed

+120
-4
lines changed

5 files changed

+120
-4
lines changed

js/profit_targets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2022-03-19 17:01:38
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2022-03-19 18:14:06
5+
* @Last Modified time: 2022-04-11 10:57:05
66
*/
77

88
/**
181 KB
Loading
Binary file not shown.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* @Author: Chacha
3+
* @Date: 2022-04-11 11:06:31
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2022-04-11 13:19:37
6+
*/
7+
8+
/**
9+
* 来源:https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/
10+
*
11+
* 在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。
12+
* 由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横坐标就足够了。开始坐标总是小于结束坐标。
13+
*
14+
* 一支弓箭可以沿着 x 轴从不同点完全垂直地射出。在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend,
15+
* 且满足 xstart ≤ x ≤ xend,则该气球会被引爆。可以射出的弓箭的数量没有限制。弓箭一旦被射出之后,可以无限地前进。
16+
* 我们想找到使得所有气球全部被引爆,所需的弓箭的最小数量。
17+
*
18+
* 给你一个数组 points ,其中 points [i] = [xstart,xend] ,返回引爆所有气球所必须射出的最小弓箭数。
19+
*
20+
* 示例 1:
21+
* 输入:points = [[10,16],[2,8],[1,6],[7,12]]
22+
* 输出:2
23+
* 解释:对于该样例,x = 6 可以射爆 [2,8],[1,6] 两个气球,以及 x = 11 射爆另外两个气球
24+
*
25+
* 示例 2:
26+
* 输入:points = [[1,2],[3,4],[5,6],[7,8]]
27+
* 输出:4
28+
*
29+
* 示例 3:
30+
* 输入:points = [[1,2],[2,3],[3,4],[4,5]]
31+
* 输出:2
32+
*
33+
* 示例 4:
34+
* 输入:points = [[1,2]]
35+
* 输出:1
36+
*
37+
* 示例 5:
38+
* 输入:points = [[2,3],[2,3]]
39+
* 输出:1
40+
*
41+
* 提示:
42+
* 0 <= points.length <= 104
43+
* points[i].length == 2
44+
* -2^31 <= xstart < xend <= 2^31 - 1
45+
*
46+
*/
47+
48+
#include <iostream>
49+
#include <vector>
50+
51+
using namespace std;
52+
53+
class Solution
54+
{
55+
private:
56+
/* data */
57+
public:
58+
int findMinArrowShots(vector<vector<int>> &points);
59+
};
60+
61+
/**
62+
* 排序 + 贪心
63+
*
64+
* 时间复杂度:O(nlogn),其中 n 是数组 points 的长度。排序的时间复杂度为 O(nlogn),
65+
* 对所有气球进行遍历并计算答案的时间复杂度为 O(n),其在渐进意义下小于前者,因此可以忽略。
66+
* 空间复杂度:O(logn),即为排序需要使用的栈空间。
67+
*
68+
*/
69+
int Solution::findMinArrowShots(vector<vector<int>> &points)
70+
{
71+
if (points.empty())
72+
{
73+
return 0;
74+
}
75+
76+
// 将 points 按照 y 值(右边界)进行升序排序
77+
// 比如 [[10,16],[2,8],[1,6],[7,12]] -> [[1,6],[2,8],[7,12],[10,16]]
78+
sort(points.begin(), points.end(), [](const vector<int> &u, const vector<int> &v)
79+
{ return u[1] < v[1]; });
80+
81+
int pos = points[0][1];
82+
int ans = 1;
83+
84+
for (const vector<int> &balloon : points)
85+
{
86+
if (balloon[0] > pos)
87+
{
88+
pos = balloon[1];
89+
++ans;
90+
}
91+
}
92+
93+
return ans;
94+
}
95+
96+
int main(int argc, char const *argv[])
97+
{
98+
Solution s;
99+
vector<vector<int>> a;
100+
vector<vector<int>> b;
101+
102+
a[0] = vector<int>(10, 16);
103+
a[1] = vector<int>(2, 8);
104+
a[2] = vector<int>(1, 6);
105+
a[3] = vector<int>(7, 12);
106+
107+
b[0] = vector<int>(1, 2);
108+
b[1] = vector<int>(2, 3);
109+
b[2] = vector<int>(3, 4);
110+
b[3] = vector<int>(4, 5);
111+
112+
cout << "[[10,16],[2,8],[1,6],[7,12]] findMinArrowShots" << s.findMinArrowShots(a) << endl;
113+
cout << "[[1,2],[3,4],[5,6],[7,8]] findMinArrowShots" << s.findMinArrowShots(b) << endl;
114+
115+
return 0;
116+
}

leetcode/two_pointers/container_with_most_water.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2022-04-01 21:20:42
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2022-04-01 22:10:08
5+
* @Last Modified time: 2022-04-11 11:22:49
66
*/
77

88
/**
@@ -20,8 +20,8 @@
2020
*
2121
* 提示:
2222
* 1. n == height.length
23-
* 2. 2 <= n <= 105
24-
* 3. 0 <= height[i] <= 104
23+
* 2. 2 <= n <= 10^5
24+
* 3. 0 <= height[i] <= 10^4
2525
*
2626
*/
2727

0 commit comments

Comments
 (0)