Problem 250759 · easy · Phase 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 = ["5", "2", "COMBO", "VOID", "DOUBLE"]
Output: 11
Explanation: 5, 2, then 7 (combo), void removes the 7, then double gives 4. Ledger: 5 + 2 + 4 = 11.

Input:  ops = ["1", "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…