You are building a core component for a real-time analytics system that tracks the profitability of experiences on a large online platform.
Implement a function that processes a sequence of operations and returns the result of each query.
You are given three arrays of equal length:
-
operations[i]
: either
'U'
for an update or
'Q'
for a query
-
experiences[i]
: the name of the experience involved in operation
i
-
deltas[i]
: the profit change for the experience if
operations[i] == 'U'
; otherwise this value is ignored
Rules:
-
For an update operation
'U'
, add
deltas[i]
to the current total earnings of
experiences[i]
-
For a query operation
'Q'
, return the name of the experience with the highest total earnings at that moment
-
If an experience has not been updated before, treat its earnings as
0
before applying its first update
-
If multiple experiences are tied for the highest earnings, returning any one of them is acceptable
-
You may assume each query happens after at least one update
Return an array containing the answers for all query operations, in order.
Example:
-
operations = ['U', 'U', 'Q', 'U', 'Q']
-
experiences = ['GameA', 'GameB', '', 'GameA', '']
-
deltas = [5, 7, 0, 4, 0]
Processing:
-
Update
GameA
by
+5
→ totals:
GameA = 5
-
Update
GameB
by
+7
→ totals:
GameB = 7
-
Query → return
GameB
-
Update
GameA
by
+4
→ totals:
GameA = 9
-
Query → return
GameA
Output: ['GameB', 'GameA']
Design an efficient solution for processing many updates and queries.