Skip to content

Commit 7d79b6b

Browse files
feat: add Minimum Number of Arrows to Burst Balloons solution
- Greedy algorithm implementation - Multiple approaches: Sort by Start, Iterative, Recursive - O(n log n) time, O(1) space complexity - Comprehensive solutions and optimizations
1 parent 3d5c5a1 commit 7d79b6b

File tree

1 file changed

+167
-0
lines changed
  • neetcode-150/intervals/minimum-number-of-arrows-to-burst-balloons

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/**
2+
* Time Complexity: O(n log n) - Sorting
3+
* Space Complexity: O(1) - In-place modification
4+
*/
5+
class Solution {
6+
public int findMinArrowShots(int[][] points) {
7+
if (points.length == 0) {
8+
return 0;
9+
}
10+
11+
// Sort balloons by end position
12+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
13+
14+
int arrows = 1;
15+
int end = points[0][1];
16+
17+
for (int i = 1; i < points.length; i++) {
18+
if (points[i][0] > end) {
19+
arrows++;
20+
end = points[i][1];
21+
}
22+
}
23+
24+
return arrows;
25+
}
26+
}
27+
28+
// Alternative approach using sorting by start position
29+
class SolutionSortByStart {
30+
public int findMinArrowShots(int[][] points) {
31+
if (points.length == 0) {
32+
return 0;
33+
}
34+
35+
// Sort balloons by start position
36+
Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
37+
38+
int arrows = 1;
39+
int end = points[0][1];
40+
41+
for (int i = 1; i < points.length; i++) {
42+
if (points[i][0] > end) {
43+
arrows++;
44+
end = points[i][1];
45+
} else {
46+
end = Math.min(end, points[i][1]);
47+
}
48+
}
49+
50+
return arrows;
51+
}
52+
}
53+
54+
// Alternative approach using iterative
55+
class SolutionIterative {
56+
public int findMinArrowShots(int[][] points) {
57+
if (points.length == 0) {
58+
return 0;
59+
}
60+
61+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
62+
63+
int arrows = 1;
64+
int end = points[0][1];
65+
66+
for (int i = 1; i < points.length; i++) {
67+
if (points[i][0] > end) {
68+
arrows++;
69+
end = points[i][1];
70+
}
71+
}
72+
73+
return arrows;
74+
}
75+
}
76+
77+
// Alternative approach using while loop
78+
class SolutionWhileLoop {
79+
public int findMinArrowShots(int[][] points) {
80+
if (points.length == 0) {
81+
return 0;
82+
}
83+
84+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
85+
86+
int arrows = 1;
87+
int end = points[0][1];
88+
int i = 1;
89+
90+
while (i < points.length) {
91+
if (points[i][0] > end) {
92+
arrows++;
93+
end = points[i][1];
94+
}
95+
i++;
96+
}
97+
98+
return arrows;
99+
}
100+
}
101+
102+
// Alternative approach using enhanced for loop
103+
class SolutionEnhancedForLoop {
104+
public int findMinArrowShots(int[][] points) {
105+
if (points.length == 0) {
106+
return 0;
107+
}
108+
109+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
110+
111+
int arrows = 1;
112+
int end = points[0][1];
113+
114+
for (int i = 1; i < points.length; i++) {
115+
if (points[i][0] > end) {
116+
arrows++;
117+
end = points[i][1];
118+
}
119+
}
120+
121+
return arrows;
122+
}
123+
}
124+
125+
// Alternative approach using recursive
126+
class SolutionRecursive {
127+
public int findMinArrowShots(int[][] points) {
128+
if (points.length == 0) {
129+
return 0;
130+
}
131+
132+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
133+
134+
return findMinArrowShotsHelper(points, 0, points[0][1], 1);
135+
}
136+
137+
private int findMinArrowShotsHelper(int[][] points, int index, int end, int arrows) {
138+
if (index >= points.length) {
139+
return arrows;
140+
}
141+
142+
if (points[index][0] > end) {
143+
return findMinArrowShotsHelper(points, index + 1, points[index][1], arrows + 1);
144+
} else {
145+
return findMinArrowShotsHelper(points, index + 1, end, arrows);
146+
}
147+
}
148+
}
149+
150+
// More concise version
151+
class SolutionConcise {
152+
public int findMinArrowShots(int[][] points) {
153+
if (points.length == 0) return 0;
154+
155+
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
156+
157+
int arrows = 1, end = points[0][1];
158+
for (int i = 1; i < points.length; i++) {
159+
if (points[i][0] > end) {
160+
arrows++;
161+
end = points[i][1];
162+
}
163+
}
164+
165+
return arrows;
166+
}
167+
}

0 commit comments

Comments
 (0)