Skip to content

Commit 8fdbc74

Browse files
committed
feat: add solutions to lc problems: No.0452,0757
* No.0452.Minimum Number of Arrows to Burst Balloons * No.0757.Set Intersection Size At Least Two
1 parent db7d6e6 commit 8fdbc74

File tree

12 files changed

+486
-16
lines changed

12 files changed

+486
-16
lines changed

solution/0400-0499/0452.Minimum Number of Arrows to Burst Balloons/README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,92 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:排序 + 贪心**
58+
59+
相似题目:[757. 设置交集大小至少为 2](/solution/0700-0799/0757.Set%20Intersection%20Size%20At%20Least%20Two/README.md)
60+
5761
<!-- tabs:start -->
5862

5963
### **Python3**
6064

6165
<!-- 这里可写当前语言的特殊实现逻辑 -->
6266

6367
```python
64-
68+
class Solution:
69+
def findMinArrowShots(self, points: List[List[int]]) -> int:
70+
points.sort(key=lambda x: x[1])
71+
ans = 1
72+
x = points[0][1]
73+
for a, b in points:
74+
if a > x:
75+
ans += 1
76+
x = b
77+
return ans
6578
```
6679

6780
### **Java**
6881

6982
<!-- 这里可写当前语言的特殊实现逻辑 -->
7083

7184
```java
85+
class Solution {
86+
public int findMinArrowShots(int[][] points) {
87+
Arrays.sort(points, (a, b) -> a[1] < b[1] ? -1 : 1);
88+
int ans = 1;
89+
int x = points[0][1];
90+
for (int[] v : points) {
91+
int a = v[0], b = v[1];
92+
if (a > x) {
93+
++ans;
94+
x = b;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int findMinArrowShots(vector<vector<int>>& points) {
108+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
109+
return a[1] < b[1];
110+
});
111+
int ans = 1;
112+
int x = points[0][1];
113+
for (auto& v : points)
114+
{
115+
int a = v[0], b = v[1];
116+
if (a > x)
117+
{
118+
++ans;
119+
x = b;
120+
}
121+
}
122+
return ans;
123+
}
124+
};
125+
```
72126
127+
### **Go**
128+
129+
```go
130+
func findMinArrowShots(points [][]int) int {
131+
sort.Slice(points, func(i, j int) bool { return points[i][1] < points[j][1] })
132+
ans := 1
133+
x := points[0][1]
134+
for _, v := range points {
135+
a, b := v[0], v[1]
136+
if a > x {
137+
ans++
138+
x = b
139+
}
140+
}
141+
return ans
142+
}
73143
```
74144

75145
### **...**

solution/0400-0499/0452.Minimum Number of Arrows to Burst Balloons/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,79 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def findMinArrowShots(self, points: List[List[int]]) -> int:
60+
points.sort(key=lambda x: x[1])
61+
ans = 1
62+
x = points[0][1]
63+
for a, b in points:
64+
if a > x:
65+
ans += 1
66+
x = b
67+
return ans
5968
```
6069

6170
### **Java**
6271

6372
```java
73+
class Solution {
74+
public int findMinArrowShots(int[][] points) {
75+
Arrays.sort(points, (a, b) -> a[1] < b[1] ? -1 : 1);
76+
int ans = 1;
77+
int x = points[0][1];
78+
for (int[] v : points) {
79+
int a = v[0], b = v[1];
80+
if (a > x) {
81+
++ans;
82+
x = b;
83+
}
84+
}
85+
return ans;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int findMinArrowShots(vector<vector<int>>& points) {
96+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
97+
return a[1] < b[1];
98+
});
99+
int ans = 1;
100+
int x = points[0][1];
101+
for (auto& v : points)
102+
{
103+
int a = v[0], b = v[1];
104+
if (a > x)
105+
{
106+
++ans;
107+
x = b;
108+
}
109+
}
110+
return ans;
111+
}
112+
};
113+
```
64114
115+
### **Go**
116+
117+
```go
118+
func findMinArrowShots(points [][]int) int {
119+
sort.Slice(points, func(i, j int) bool { return points[i][1] < points[j][1] })
120+
ans := 1
121+
x := points[0][1]
122+
for _, v := range points {
123+
a, b := v[0], v[1]
124+
if a > x {
125+
ans++
126+
x = b
127+
}
128+
}
129+
return ans
130+
}
65131
```
66132

67133
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int findMinArrowShots(vector<vector<int>>& points) {
4+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
5+
return a[1] < b[1];
6+
});
7+
int ans = 1;
8+
int x = points[0][1];
9+
for (auto& v : points)
10+
{
11+
int a = v[0], b = v[1];
12+
if (a > x)
13+
{
14+
++ans;
15+
x = b;
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func findMinArrowShots(points [][]int) int {
2+
sort.Slice(points, func(i, j int) bool { return points[i][1] < points[j][1] })
3+
ans := 1
4+
x := points[0][1]
5+
for _, v := range points {
6+
a, b := v[0], v[1]
7+
if a > x {
8+
ans++
9+
x = b
10+
}
11+
}
12+
return ans
13+
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
22
public int findMinArrowShots(int[][] points) {
3-
if (points == null || points.length == 0) {
4-
return 0;
5-
}
6-
Arrays.sort(points, Comparator.comparingInt(o -> o[1]));
7-
int res = 1;
8-
int pre = points[0][1];
9-
for (int i = 1; i < points.length; ++i) {
10-
if (points[i][0] > pre) {
11-
++res;
12-
pre = points[i][1];
3+
Arrays.sort(points, (a, b) -> a[1] < b[1] ? -1 : 1);
4+
int ans = 1;
5+
int x = points[0][1];
6+
for (int[] v : points) {
7+
int a = v[0], b = v[1];
8+
if (a > x) {
9+
++ans;
10+
x = b;
1311
}
1412
}
15-
return res;
13+
return ans;
1614
}
17-
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findMinArrowShots(self, points: List[List[int]]) -> int:
3+
points.sort(key=lambda x: x[1])
4+
ans = 1
5+
x = points[0][1]
6+
for a, b in points:
7+
if a > x:
8+
ans += 1
9+
x = b
10+
return ans

solution/0700-0799/0757.Set Intersection Size At Least Two/README.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,125 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:排序 + 贪心**
45+
46+
相似题目: [452. 用最少数量的箭引爆气球](/solution/0400-0499/0452.Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/README.md)
47+
4448
<!-- tabs:start -->
4549

4650
### **Python3**
4751

4852
<!-- 这里可写当前语言的特殊实现逻辑 -->
4953

5054
```python
51-
55+
class Solution:
56+
def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:
57+
intervals.sort(key=lambda x: (x[1], -x[0]))
58+
s = e = -1
59+
ans = 0
60+
for a, b in intervals:
61+
if a <= s:
62+
continue
63+
if a > e:
64+
ans += 2
65+
s, e = b - 1, b
66+
else:
67+
ans += 1
68+
s, e = e, b
69+
return ans
5270
```
5371

5472
### **Java**
5573

5674
<!-- 这里可写当前语言的特殊实现逻辑 -->
5775

5876
```java
77+
class Solution {
78+
public int intersectionSizeTwo(int[][] intervals) {
79+
Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0] : a[1] - b[1]);
80+
int ans = 0;
81+
int s = -1, e = -1;
82+
for (int[] v : intervals) {
83+
int a = v[0], b = v[1];
84+
if (a <= s) {
85+
continue;
86+
}
87+
if (a > e) {
88+
ans += 2;
89+
s = b - 1;
90+
e = b;
91+
} else {
92+
ans += 1;
93+
s = e;
94+
e = b;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int intersectionSizeTwo(vector<vector<int>>& intervals) {
108+
sort(intervals.begin(), intervals.end(), [&](vector<int>& a, vector<int>& b) {
109+
return a[1] == b[1] ? a[0] > b[0] : a[1] < b[1];
110+
});
111+
int ans = 0;
112+
int s = -1, e = -1;
113+
for (auto& v : intervals)
114+
{
115+
int a = v[0], b = v[1];
116+
if (a <= s) continue;
117+
if (a > e)
118+
{
119+
ans += 2;
120+
s = b - 1;
121+
e = b;
122+
}
123+
else
124+
{
125+
ans += 1;
126+
s = e;
127+
e = b;
128+
}
129+
}
130+
return ans;
131+
}
132+
};
133+
```
59134
135+
### **Go**
136+
137+
```go
138+
func intersectionSizeTwo(intervals [][]int) int {
139+
sort.Slice(intervals, func(i, j int) bool {
140+
a, b := intervals[i], intervals[j]
141+
if a[1] == b[1] {
142+
return a[0] > b[0]
143+
}
144+
return a[1] < b[1]
145+
})
146+
ans := 0
147+
s, e := -1, -1
148+
for _, v := range intervals {
149+
a, b := v[0], v[1]
150+
if a <= s {
151+
continue
152+
}
153+
if a > e {
154+
ans += 2
155+
s, e = b - 1, b
156+
} else {
157+
ans += 1
158+
s, e = e, b
159+
}
160+
}
161+
return ans
162+
}
60163
```
61164

62165
### **...**

0 commit comments

Comments
 (0)