Skip to content

Commit beac6ef

Browse files
authored
Create 0682-baseball-game.kt
1 parent e7c8da7 commit beac6ef

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

kotlin/0682-baseball-game.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Using a stack. Time Complexity O(N) and Space Complexity O(N)
3+
*/
4+
class Solution {
5+
fun calPoints(operations: Array<String>): Int {
6+
7+
val stack = ArrayDeque<Int>()
8+
9+
for(op in operations) {
10+
when (op) {
11+
"+" -> {
12+
val top = stack.removeLast()
13+
val sum = stack.peekLast() + top
14+
stack.addLast(top)
15+
stack.addLast(sum)
16+
}
17+
"D" -> {
18+
val top = stack.peekLast() * 2
19+
stack.addLast(top)
20+
}
21+
"C" -> {
22+
stack.removeLast()
23+
}
24+
else -> {
25+
stack.addLast(op.toInt())
26+
}
27+
}
28+
}
29+
30+
return stack.sum()
31+
}
32+
}

0 commit comments

Comments
 (0)