leetle

Daily challenge for 2025-05-23

143. Baseball Game

Write a function solve that calculates the total score of a baseball game given operations (integer, +, D, C). An integer n means you record the score. + means record the sum of the last two scores, D means record the double of the last score, and C means cancel the last score. Finally, you should sum the scores.

Example:
Input: ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.

Run this challenge interactively in Python or JavaScript when the app loads.