-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
833cd22
commit 264b647
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |