Skip to content

Commit 9a565a4

Browse files
committed
[Array] Add a solution to Asteroid Collision
1 parent 549eb1f commit 9a565a4

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Array/AsteroidCollision.swift

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/asteroid-collision/
3+
* Primary idea: traverse the array and handle positive and negative separately
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class AsteroidCollision {
10+
func asteroidCollision(_ asteroids: [Int]) -> [Int] {
11+
var positives = [Int]()
12+
var negatives = [Int]()
13+
14+
for asteroid in asteroids {
15+
if asteroid > 0 {
16+
positives.append(asteroid)
17+
} else {
18+
var shouldAppendToNegative = true
19+
20+
while positives.count > 0 {
21+
if positives.last! > -asteroid {
22+
shouldAppendToNegative = false
23+
break
24+
} else {
25+
let last = positives.removeLast()
26+
27+
if -asteroid == last {
28+
shouldAppendToNegative = false
29+
break
30+
}
31+
}
32+
}
33+
34+
if shouldAppendToNegative {
35+
negatives.append(asteroid)
36+
}
37+
}
38+
}
39+
40+
return negatives + positives
41+
}
42+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 256 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 257 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -54,6 +54,7 @@
5454
[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [Swift](./Array/ThreeSum.swift)| Medium| O(n^2)| O(nC3)|
5555
[4Sum](https://leetcode.com/problems/4sum/)| [Swift](./Array/FourSum.swift)| Medium| O(n^3)| O(nC4)|
5656
[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [Swift](./Array/SummaryRanges.swift)| Medium| O(n)| O(n)|
57+
[Asteroid Collision](https://leetcode.com/problems/asteroid-collision/)| [Swift](./Array/AsteroidCollision.swift)| Medium| O(n)| O(n)|
5758
[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)| [Swift](./Array/ShortestWordDistance.swift)| Easy| O(n)| O(1)|
5859
[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)| [Swift](./Array/ShortestWordDistanceIII.swift)| Medium| O(n)| O(1)|
5960
[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [Swift](./Array/MinimumSizeSubarraySum.swift)| Medium| O(n)| O(1)|

0 commit comments

Comments
 (0)