Show problems as
Classic problem #682EasyArrayStackSimulation

Baseball Game

Real-world version: Tallying a Round With Corrections
Tracked in this browser only
Read Baseball Game on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstTwo Stacks · Stacks & Queues, animated step by step

A scorer's sheet reads entries, worked through in order. Each entry is one of the following:

  • a whole number written as text: record that score;
  • "+": record a score equal to the last two recorded scores added together;
  • "D": record a score equal to twice the last recorded score;
  • "C": strike out the last recorded score, as though it had never been recorded.

Return all the recorded scores added together once the whole sheet has been worked through.

Every "+" has at least two scores already recorded, and every "D" and "C" at least one.

Examples

Example 1

Input
entries = ["3", "D", "D"]
Output
21

Three is recorded, then twice it, six, then twice that, twelve. Three, six and twelve add to twenty-one.

Example 2

Input
entries = ["-5", "D", "+"]
Output
-30

Minus five is recorded, then twice it, minus ten, then those two added, minus fifteen. The three together come to minus thirty.

Example 3

Input
entries = ["10"]
Output
10

One entry and one recorded score, so the total is that score.

Constraints

  • 1 <= entries.length <= 1000
  • Each entry is "C", "D", "+", or a whole number written as text between -30000 and 30000.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def cal_points(entries: list[str]) -> int:
Java
public int calPoints(String[] entries)