Terrible LeetCode in Kotlin:

Опубликовано: 02 Май 2026
на канале: Personal Finance with Raymond
36
2

Another day, another LeetCode question (for now). Today, I'm running through #735 Asteroid Collision. The problem provides an array of elements where the magnitude represents an asteroid size and the sign its direction, with the expected result being an integer array that represents the state of the asteroids after all collisions have occurred.

My instinct made me think of a potential two-pass solution by first evaluating all of the collisions left to right, then right to left, but I couldn't get it to work with all cases. Like the previous problem, I ended up looking at the solution after the 30 minutes of so of attempting the problem.

The solution was actually a lot simpler than I expected. The solution builds itself up from the first asteroid, and simply evaluates all of the asteroid collisions one at a time from left to right in a stack. This results in an O(n) runtime algorithm with O(n) space, where n is the number of elements within the array.

I work in Kotlin as my LeetCode programming language because my current focus is on Android app development. Kotlin is interoperable with Java, so the solution should be relatively similar albeit with some more boilerplate checks for nulls and such.

Current Grind:
Grind75 list, 22 weeks/16 hours per week, [110/169]
(https://www.techinterviewhandbook.org...)

Chapters:
00:00 - Problem discussion
14:30 - Coding Begins (Attempt at Sliding Window/Two Pointers)
39:48 - Solution Discussion (Stack)