Skip to content

Commit ee7c8c9

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 735
1 parent 738a2ba commit ee7c8c9

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
- [712 Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/)
245245
- [714 Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
246246
- [724 Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/)
247+
- [735 Asteroid Collision](https://leetcode.com/problems/asteroid-collision/description/)
247248
- [740 Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/)
248249
- [746 Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
249250
- [790 Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/description/)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
8+
"""
9+
We are given an array asteroids of integers representing asteroids in a row.
10+
The indices of the asteriod in the array represent their relative position in
11+
space.
12+
13+
For each asteroid, the absolute value represents its size, and the sign
14+
represents its direction (positive meaning right, negative meaning left).
15+
Each asteroid moves at the same speed.
16+
17+
Find out the state of the asteroids after all collisions. If two asteroids meet,
18+
the smaller one will explode. If both are the same size, both will explode.
19+
Two asteroids moving in the same direction will never meet.
20+
"""
21+
# Time Complexity: O(n)
22+
# Space Complexity: O(n)
23+
res = []
24+
for asteroid in asteroids:
25+
while res and asteroid < 0 and res[-1] > 0:
26+
diff = asteroid + res[-1]
27+
if diff < 0:
28+
# abs(asteroid) > abs(res[-1])
29+
# res destroyed
30+
res.pop()
31+
elif diff > 0:
32+
# abs(asteroid) < abs(res[-1])
33+
# asteroid destroyed
34+
asteroid = 0
35+
else:
36+
# abs(asteroid) == abs(res[-1])
37+
# both destroyed
38+
asteroid = 0
39+
res.pop()
40+
if asteroid:
41+
res.append(asteroid)
42+
return res

tests/test_735_asteroid_collision.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._735_asteroid_collision import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["asteroids", "expected"],
10+
argvalues=[
11+
([5, 10, -5], [5, 10]),
12+
([8, -8], []),
13+
([10, 2, -5], [10]),
14+
],
15+
)
16+
def test_func(asteroids: List[int], expected: List[int]):
17+
"""Tests the solution of a LeetCode problem."""
18+
asteroids_left = Solution().asteroidCollision(asteroids)
19+
assert asteroids_left == expected

0 commit comments

Comments
 (0)