Baseball Game

Another problem with a very straighforward answer.

class Solution {
    fun calPoints(ops: Array<String>): Int {
        var list = mutableListOf<Int>()
        ops.forEach {
            when {
                it == "+" -> if(!list.isEmpty() && list.size > 1) list.add(list.last() + list[list.size - 2])
                it == "D" -> if(!list.isEmpty()) list.add(list.last() * 2)
                it == "C" -> if(!list.isEmpty()) list.removeAt(list.size - 1)
                else -> {
                    var op = it
                    var positive = true
                    if(op.startsWith("-")){
                        op = op.replace("-","")
                        positive = false
                    }
                    
                    if(Character.isDigit(op.toCharArray()[0])) list.add(op.toInt() * if(positive) 1 else -1)
                }
            }
        }
        
        return list.sum()
    }
}
Runtime: 248 ms, faster than 25.00% of Kotlin online submissions for Baseball Game.
Memory Usage: 37.9 MB, less than 100.00% of Kotlin online submissions for Baseball Game.