Baseball Game

Easy · Stack & Monotonic Stack · about 10 minutes

Asked at Amazon, Bloomberg, Meta, Google, Microsoft.

You're recording scores under some unusual rules. Go through operations, where each entry is one of:

  • an integer x: add a score of x to the record;
  • "+": add a score equal to the last two scores added together;
  • "D": add a score equal to twice the last score;
  • "C": cancel the last score, removing it from the record.

Every operation is valid when it's applied. Return the total of all scores left on the record.