Implement fun asteroidCollision(asteroids: IntArray): IntArray else if (Math.abs(prev) > Math. If there are no collisions, decrement the stack curIndex.Only decrement when curIndex is at the end of the stack. Given a collision with one AST with a larger value, remove the smaller AST.If the curIndex is not at the end of the stack after removing ASTs, continue to check for right-most collisions by decrementing curIndex. Given a collision with ASTs with equal value, remove both ASTs.A collision occurs when prev is positive and cur is negative. Using the current cur and previous prev values, check whether a collision will occur.Start curIndex at the top of the stack and check the stack until curIndex is at the second element of the stack, thus checking for all possible AST collisions.Build ArrayList used as a stack and add the AST values to the stack.However, I'm looking to improve speed and memory performance. The solution performs as expected in terms of achieving the correct results. How arrayList Works in Kotlin The arrayList is one of the collection class in the kotlin language it is mainly used for to store the datas in a dynamic format. The 5 and 10 never collide.Ĭreate a stack using an ArrayList to compare asteroid (AST) direction, values, and remove ASTs. Smaller ASTs explode (are deleted), the same size both explode, and same direction ASTs do nothingĮxplanation: The 10 and -5 collide resulting in 10.Negative numbers move left, positive numbers move right.Is this a performant strategy to implement a stack in Kotlin to compare and delete values? ExpectĬreate a stack in order to compare an array of asteroids (ASTs) and handle collisions (deletions), returning the final array of ASTs after collisions.
0 Comments
Leave a Reply. |