We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e7c8da7 commit beac6efCopy full SHA for beac6ef
kotlin/0682-baseball-game.kt
@@ -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
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