Problem 250759 · easy · Level 02 Linear Data Structures

Card Game Ledger

stacks · simulation

A card game keeps a ledger of round scores. You receive a list of string tokens ops describing, in order, what happened each round:

  • an integer string such as "5" or "-3": record that score;
  • "DOUBLE": record a new score equal to twice the most recent score;
  • "COMBO": record a new score equal to the sum of the two most recent scores;
  • "VOID": cancel the most recent score (remove it from the ledger).

All operations are valid (there is always a recent score to double or void, and two scores to combo). Return the sum of all scores remaining in the ledger.

Examples

Input:  ops = ["6", "3", "DOUBLE", "VOID", "COMBO"]
Output: 18
Explanation: 6, 3, then 6 (double), void removes that 6, then combo gives 9. Ledger: 6 + 3 + 9 = 18.

Input:  ops = ["8", "DOUBLE", "VOID", "VOID"]
Output: 0

Constraints

  • 1 <= len(ops) <= 10**4
  • Target: O(n) time

Goals

  • Translate each token into a stack operation
  • Access the top two elements without removing them
Starting Python…