Skip to content

Commit

Permalink
252nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Nov 10, 2024
1 parent 9204ace commit a326806
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ Ace Coding Interview with 75 Qs
| Intervals | | |
| ----------------------------------------------- | --------------- | ------ |
| 435. Non-overlapping Intervals | [Solution][435] | Medium |
| 452. Minimum Number of Arrows to Burst Balloons | Solution | Medium |
| 452. Minimum Number of Arrows to Burst Balloons | [Solution][452] | Medium |

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

| Monotonic Stack | | |
| ----------------------- | -------- | ------ |
Expand Down Expand Up @@ -385,12 +386,14 @@ Must-do List for Interview Prep

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

| Intervals | | |
| ----------------------------------------------- | -------- | ------ |
| 228. Summary Ranges | Solution | Easy |
| 56. Merge Intervals | Solution | Medium |
| 57. Insert Interval | Solution | Medium |
| 452. Minimum Number of Arrows to Burst Balloons | Solution | Medium |
| Intervals | | |
| ----------------------------------------------- | --------------- | ------ |
| 228. Summary Ranges | Solution | Easy |
| 56. Merge Intervals | Solution | Medium |
| 57. Insert Interval | Solution | Medium |
| 452. Minimum Number of Arrows to Burst Balloons | [Solution][452] | Medium |

[452]: ./src/page-5/452.%20Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/findMinArrowShots.ts

| Stack | | |
| ------------------------------------- | --------------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { findMinArrowShots } from './findMinArrowShots';

describe('452. Minimum Number of Arrows to Burst Balloons', () => {
test('findMinArrowShots', () => {
expect(
findMinArrowShots([
[10, 16],
[2, 8],
[1, 6],
[7, 12],
]),
).toBe(2);

expect(
findMinArrowShots([
[1, 2],
[3, 4],
[5, 6],
[7, 8],
]),
).toBe(4);

expect(
findMinArrowShots([
[1, 2],
[2, 3],
[3, 4],
[4, 5],
]),
).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type FindMinArrowShots = (points: number[][]) => number;

/**
* Accepted
*/
export const findMinArrowShots: FindMinArrowShots = (points) => {
// Sort the balloons by their end positions
points.sort((a, b) => a[1] - b[1]);

// Initialize the number of arrows needed and the position of the last arrow shot
let arrows = 1;
let arrowPosition = points[0][1];

// Iterate through each balloon
for (let i = 1; i < points.length; i++) {
// If the current balloon's start is greater than the last arrow position
if (points[i][0] > arrowPosition) {
// We need a new arrow
arrows += 1;
arrowPosition = points[i][1]; // Update the arrow position to the current balloon's end
}
}

return arrows;
};

0 comments on commit a326806

Please sign in to comment.