Skip to content

Commit 14515cc

Browse files
committed
Add: Exercise 735. Asteroid Collision
1 parent f6a1f01 commit 14515cc

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

735-asteroid-collision/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 735. Asteroid Collision
2+
We are given an array `asteroids` of integers representing asteroids in a row.
3+
4+
For each asteroid, the absolute value represents its size, and the sign represents its direction
5+
(positive meaning right, negative meaning left). Each asteroid moves at the same speed.
6+
7+
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one
8+
will explode. If both are the same size, both will explode. Two asteroids moving in the same
9+
direction will never meet.
10+
11+
## Examples
12+
13+
### Example 1:
14+
15+
Input: `asteroids` = [5,10,-5]
16+
Output: [5,10]
17+
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
18+
19+
### Example 2:
20+
21+
Input: `asteroids` = [8,-8]
22+
Output: []
23+
Explanation: The 8 and -8 collide exploding each other.
24+
25+
### Example 3:
26+
27+
Input: `asteroids` = [10,2,-5]
28+
Output: [10]
29+
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
30+
31+
## Constraints
32+
33+
- 2 <= `asteroids.length` <= 10^4
34+
- -1000 <= `asteroids[i]` <= 1000
35+
- `asteroids[i]` != 0

735-asteroid-collision/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} asteroids
3+
* @return {number[]}
4+
*/
5+
var asteroidCollision = function(asteroids) {
6+
const asteroidsStack = [asteroids[0]];
7+
asteroidsStack.peek = () => asteroidsStack[asteroidsStack.length - 1];
8+
9+
let i = 1;
10+
while (i < asteroids.length) {
11+
// Detect collisions
12+
// If two asteroids meet, the smaller one (by absolute value) will explode
13+
if (asteroidsStack.peek() > 0 && asteroids[i] < 0) {
14+
// Iterate until all collisions are resolved
15+
while (asteroidsStack.peek() > 0 && asteroids[i] < 0) {
16+
// If both are the same size (by absolute value), both will explode
17+
if (asteroidsStack.peek() + asteroids[i] === 0) {
18+
asteroidsStack.pop();
19+
i++;
20+
// If the incoming asteroid is larger (moving to the left), remove the top of the stack
21+
} else if (asteroidsStack.peek() + asteroids[i] < 0) {
22+
asteroidsStack.pop();
23+
// If the asteroid in the stack is larger (moving to the right), ignore the incoming asteroid
24+
} else {
25+
i++;
26+
}
27+
}
28+
// If there's no collision, add the asteroid to the stack
29+
} else {
30+
asteroidsStack.push(asteroids[i]);
31+
i++;
32+
}
33+
}
34+
35+
delete asteroidsStack.peek
36+
37+
return asteroidsStack;
38+
};
39+
40+
module.exports = asteroidCollision

735-asteroid-collision/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "735-asteroid-collision",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

735-asteroid-collision/test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const assert = require('assert')
2+
const asteroidCollision = require('./index')
3+
4+
const tests = [
5+
{
6+
asteroids: [5,10,-5],
7+
expectedOutput: [5,10]
8+
},
9+
{
10+
asteroids: [8,-8],
11+
expectedOutput: []
12+
},
13+
{
14+
asteroids: [10,2,-5],
15+
expectedOutput: [10]
16+
},
17+
]
18+
19+
for (const t of tests) {
20+
assert.strict.deepEqual(asteroidCollision(t.asteroids), t.expectedOutput)
21+
}

0 commit comments

Comments
 (0)