Skip to content

Commit

Permalink
186th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 3, 2024
1 parent 833cd22 commit 264b647
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ Ace Coding Interview with 75 Qs
| Stack | | |
| ---------------------------------- | ---------------- | ------ |
| 2390. Removing Stars From a String | [Solution][2390] | Medium |
| 735. Asteroid Collision | Solution | Medium |
| 735. Asteroid Collision | [Solution][735] | Medium |
| 394. Decode String | Solution | Medium |

[2390]: ./src/page-22/2390.%20Removing%20Stars%20From%20a%20String/removeStars.ts
[735]: ./src/page-7/735.%20Asteroid%20Collision/asteroidCollision.ts

| Queue | | |
| --------------------------- | -------- | ------ |
Expand Down
9 changes: 9 additions & 0 deletions src/page-7/735. Asteroid Collision/asteroidCollision.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { asteroidCollision } from './asteroidCollision';

describe('735. Asteroid Collision', () => {
test('asteroidCollision', () => {
expect(asteroidCollision([5, 10, -5])).toStrictEqual([5, 10]);
expect(asteroidCollision([8, -8])).toStrictEqual([]);
expect(asteroidCollision([10, 2, -5])).toStrictEqual([10]);
});
});
43 changes: 43 additions & 0 deletions src/page-7/735. Asteroid Collision/asteroidCollision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
type AsteroidCollision = (asteroids: number[]) => number[];

/**
* Accepted
*/
export const asteroidCollision: AsteroidCollision = (asteroids) => {
// Stack to keep track of the remaining asteroids after collisions
const stack: number[] = [];

// Iterate over each asteroid in the input array
for (const asteroid of asteroids) {
// A flag to determine whether the current asteroid is to be added to the stack
let addAsteroid = true;

// Handle collision scenarios only if the stack is not empty and
// the current asteroid is moving left (negative) while the top of the stack is moving right (positive)
while (stack.length > 0 && stack[stack.length - 1] > 0 && asteroid < 0) {
const topAsteroid = stack[stack.length - 1];

// Check if the top asteroid is larger, if so, the current asteroid explodes
if (Math.abs(topAsteroid) > Math.abs(asteroid)) {
addAsteroid = false;
break;
}

// Check if both asteroids are of the same size, both explode
if (Math.abs(topAsteroid) === Math.abs(asteroid)) {
stack.pop();
addAsteroid = false;
break;
}

// If the top asteroid is smaller, it explodes
stack.pop();
}

// If the current asteroid survives the collisions, add it to the stack
if (addAsteroid) stack.push(asteroid);
}

// Return the state of the asteroids after all collisions
return stack;
};

0 comments on commit 264b647

Please sign in to comment.