Skip to content

Commit a326806

Browse files
committed
252nd Commit
1 parent 9204ace commit a326806

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,10 @@ Ace Coding Interview with 75 Qs
283283
| Intervals | | |
284284
| ----------------------------------------------- | --------------- | ------ |
285285
| 435. Non-overlapping Intervals | [Solution][435] | Medium |
286-
| 452. Minimum Number of Arrows to Burst Balloons | Solution | Medium |
286+
| 452. Minimum Number of Arrows to Burst Balloons | [Solution][452] | Medium |
287287

288288
[435]: ./src/page-5/435.%20Non-overlapping%20Intervals/eraseOverlapIntervals.ts
289+
[452]: ./src/page-5/452.%20Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/findMinArrowShots.ts
289290

290291
| Monotonic Stack | | |
291292
| ----------------------- | -------- | ------ |
@@ -385,12 +386,14 @@ Must-do List for Interview Prep
385386

386387
[1]: ./src/page-1/1.%20Two%20Sum/twoSum.ts
387388

388-
| Intervals | | |
389-
| ----------------------------------------------- | -------- | ------ |
390-
| 228. Summary Ranges | Solution | Easy |
391-
| 56. Merge Intervals | Solution | Medium |
392-
| 57. Insert Interval | Solution | Medium |
393-
| 452. Minimum Number of Arrows to Burst Balloons | Solution | Medium |
389+
| Intervals | | |
390+
| ----------------------------------------------- | --------------- | ------ |
391+
| 228. Summary Ranges | Solution | Easy |
392+
| 56. Merge Intervals | Solution | Medium |
393+
| 57. Insert Interval | Solution | Medium |
394+
| 452. Minimum Number of Arrows to Burst Balloons | [Solution][452] | Medium |
395+
396+
[452]: ./src/page-5/452.%20Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/findMinArrowShots.ts
394397

395398
| Stack | | |
396399
| ------------------------------------- | --------------- | ------ |
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { findMinArrowShots } from './findMinArrowShots';
2+
3+
describe('452. Minimum Number of Arrows to Burst Balloons', () => {
4+
test('findMinArrowShots', () => {
5+
expect(
6+
findMinArrowShots([
7+
[10, 16],
8+
[2, 8],
9+
[1, 6],
10+
[7, 12],
11+
]),
12+
).toBe(2);
13+
14+
expect(
15+
findMinArrowShots([
16+
[1, 2],
17+
[3, 4],
18+
[5, 6],
19+
[7, 8],
20+
]),
21+
).toBe(4);
22+
23+
expect(
24+
findMinArrowShots([
25+
[1, 2],
26+
[2, 3],
27+
[3, 4],
28+
[4, 5],
29+
]),
30+
).toBe(2);
31+
});
32+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type FindMinArrowShots = (points: number[][]) => number;
2+
3+
/**
4+
* Accepted
5+
*/
6+
export const findMinArrowShots: FindMinArrowShots = (points) => {
7+
// Sort the balloons by their end positions
8+
points.sort((a, b) => a[1] - b[1]);
9+
10+
// Initialize the number of arrows needed and the position of the last arrow shot
11+
let arrows = 1;
12+
let arrowPosition = points[0][1];
13+
14+
// Iterate through each balloon
15+
for (let i = 1; i < points.length; i++) {
16+
// If the current balloon's start is greater than the last arrow position
17+
if (points[i][0] > arrowPosition) {
18+
// We need a new arrow
19+
arrows += 1;
20+
arrowPosition = points[i][1]; // Update the arrow position to the current balloon's end
21+
}
22+
}
23+
24+
return arrows;
25+
};

0 commit comments

Comments
 (0)