-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0682-baseball-game.kt
More file actions
32 lines (29 loc) · 823 Bytes
/
0682-baseball-game.kt
File metadata and controls
32 lines (29 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* Using a stack. Time Complexity O(N) and Space Complexity O(N)
*/
class Solution {
fun calPoints(operations: Array<String>): Int {
val stack = ArrayDeque<Int>()
for(op in operations) {
when (op) {
"+" -> {
val top = stack.removeLast()
val sum = stack.peekLast() + top
stack.addLast(top)
stack.addLast(sum)
}
"D" -> {
val top = stack.peekLast() * 2
stack.addLast(top)
}
"C" -> {
stack.removeLast()
}
else -> {
stack.addLast(op.toInt())
}
}
}
return stack.sum()
}
}