Amazon Online Assessment (OA) - Baseball Scorekeeping
Alex plays a game in which he throws a baseball at various blocks marked with a symbol.
Each block comes with a symbol that can be an integer
, X
, +
, or Z
.
Given a list of strings representing blocks, return the final score.
If the block symbol is an integer
, add the integer to the total score.
If the block symbol is X
, double the score from the last throw.
If the block symbol is +
, add the scores from the last two throws.
If the block symbol is Z
, remove the score from the last throw, as though the last throw never happened.
Its value does not count towards the total score, and the subsequent throws will ignore it when computing their values.
Examples
Example 1:
Input: ["10", "20", "X", "+"]
Output: 130
Explanation:
10
-> current score =10
, total score =10
20
-> current score =20
, total score =30
X
-> current score =20 * 2 = 40
, total score =30 + 40 = 70
+
-> current score =40 + 20 = 60
, total score =70 + 60 = 130
Example 2:
Input: ["10", "20", "Z", 30, "+"]
Output: 100
Explanation:
10
-> current score =10
, total score =10
20
-> current score =20
, total score =30
Z
-> current score =10
, total score =10
(as the last throw never happened)30
-> current score =30
, total score =40
+
-> current score =30 + 10 = 40
, total score =40 + 40 = 80