Skip to main content
added flow - part
Source Link
tieskedh
  • 842
  • 5
  • 14

flow

The if-else if-else chain can be reduced to two if-statements.
This is because the case where the absolute value of cur and prev are the same is just the other two cases combined.
As far as I know, you do need more variables, to make it possible. Therefor, this is just another choice, with its own drawbacks:

fun asteroidCollision(asteroids: IntArray): IntArray {
    val stack = asteroids.toMutableList()
    var index = stack.lastIndex
    while (index >= 1) {
        val curIndex = index
        val cur = stack[curIndex]
        val prevIndex = index-1
        val prev = stack[prevIndex]
        if (prev.isRight() && cur.isLeft()) {
            if (abs(prev) >= abs(cur)){
                stack.removeAt(curIndex)
                if (index-1==stack.lastIndex)
                    index--
            }
            if (abs(cur) <= abs(prev)){
                stack.removeAt(prevIndex)
                index--
            }
        } else index--
    }
    return stack.toIntArray()
}

flow

The if-else if-else chain can be reduced to two if-statements.
This is because the case where the absolute value of cur and prev are the same is just the other two cases combined.
As far as I know, you do need more variables, to make it possible. Therefor, this is just another choice, with its own drawbacks:

fun asteroidCollision(asteroids: IntArray): IntArray {
    val stack = asteroids.toMutableList()
    var index = stack.lastIndex
    while (index >= 1) {
        val curIndex = index
        val cur = stack[curIndex]
        val prevIndex = index-1
        val prev = stack[prevIndex]
        if (prev.isRight() && cur.isLeft()) {
            if (abs(prev) >= abs(cur)){
                stack.removeAt(curIndex)
                if (index-1==stack.lastIndex)
                    index--
            }
            if (abs(cur) <= abs(prev)){
                stack.removeAt(prevIndex)
                index--
            }
        } else index--
    }
    return stack.toIntArray()
}
Source Link
tieskedh
  • 842
  • 5
  • 14

An array to list can be done by the function toList().
list.size-1 is an existing property on a list called lastIndex.

'Math.abs' is from Java. Kotlin has its own abs, which isn't prefixed by a class.

List provides an operator function to access by index: stack.get(curIndex) can be written as stack[curIndex]

fun asteroidCollision(asteroids: IntArray): IntArray {
    val stack = asteroids.toMutableList()
    var curIndex = stack.lastIndex
    while (curIndex >= 1) {
        val cur = stack[curIndex]
        val prev = stack[curIndex - 1]
        if (prev.isRight() && cur.isLeft()) {
            if (abs(cur) == abs(prev)) {
                stack.removeAt(curIndex)
                stack.removeAt(curIndex - 1)
                if (curIndex - 2 == stack.lastIndex)
                    curIndex -= 2
                else curIndex--
            } else if (abs(prev) > abs(cur)) {
                stack.removeAt(curIndex)
                if (curIndex - 1 == stack.lastIndex)
                    curIndex--
            } else {
                stack.removeAt(curIndex - 1)
                curIndex--
            }
        } else curIndex--
    }
    return stack.toIntArray()
}