Lv.1simulationstack
Score Sheet
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
A score sheet starts empty and processes the commands ops in order:
| Command | Meaning |
|---|---|
an integer x (e.g. "5", "-2") | write a new score x |
"+" | write the sum of the two most recent scores |
"D" | write double the most recent score |
"C" | erase the most recent score |
Erased scores no longer count as recent. After each command, record the sum of the scores currently on the sheet, and return these sums in command order.
Examples
| ops | result |
|---|---|
["5", "2", "C", "D", "+"] | [5, 7, 5, 15, 30] |
["5", "-2", "4", "C", "D", "9", "+", "+"] | [5, 3, 7, 3, -1, 8, 13, 27] |
["1", "C"] | [1, 0] |
Example 1 — the sheet goes [5], [5, 2], [5], [5, 10], [5, 10, 15].
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(ops: string[]) → long[]
Constraints
-
ops.length - Integer commands satisfy , without leading zeros or a
+sign. - When
"+"is given the sheet holds at least two scores; when"D"or"C"is given it holds at least one. - Every score written has absolute value at most . The sums may exceed the 32-bit range.
Expected complexity
- Time
- O(N)
- Space
- O(N)
Examples
| ops | Expected |
|---|---|
["5","2","C","D","+"] | [5,7,5,15,30] |
["5","-2","4","C","D","9","+","+"] | [5,3,7,3,-1,8,13,27] |
["1","C"] | [1,0] |
Hint
Hint
Keep the scores on a stack and maintain a running total: add each new score and subtract each erased one. Use 64-bit integers for the total. Re-adding the whole sheet after every command is and exceeds the time limit; each command can be handled in .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1538 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 3 | verdict only |