A game shows each player two numbers: their total, all the points of their turns added up, and their best single turn. Write a class ScoreKeeper that keeps both numbers up to date as the points come in, and can start over.
Implement the class ScoreKeeper:
ScoreKeeper()creates a keeper with no points yet.add(points)records the points of one turn.total()returns the sum of all the points added since the keeper was created or last reset.best()returns the highestpointsof a singleaddin that time, or0if there was noaddyet.reset()starts over: afterwardstotal()andbest()both return0until the nextadd.
The input is a list of operations, applied in order to one new ScoreKeeper. The output lists what the total and best calls return, in order. The starter code spells these names the way your language does (in Go and Rust, ScoreKeeper is a struct with methods). Give the fields names that differ from the methods' names: in several languages a field named total would clash with the method total().
Example 1:
Input:
add(10)
add(25)
add(5)
total()
best()
Output: 40 25
Explanation: 10 + 25 + 5 = 40. The best single turn was the 25.
Example 2:
Input:
best()
total()
add(0)
best()
total()
Output: 0 0 0 0
Explanation: Before any add, both numbers are 0. A turn worth 0 points changes neither.
Example 3:
Input:
add(30)
add(50)
total()
best()
reset()
total()
best()
add(20)
best()
total()
Output: 80 50 0 0 20 20
Explanation: After reset() the keeper starts over. The 50 from before no longer counts,
so after add(20) the best is 20, not 50.
Constraints:
0 <= points <= 1000- At most
300operations on oneScoreKeeper.
Note:
Every method should run in O(1) time: add updates the two numbers as each turn comes in, so total and best never need to look back at old turns.
Free plan: 30 tutor messages. PRO is unlimited.
