Design Team Experience System
Design a system that manages experience points for n players in an online game.
At the beginning:
- Every player is in their own separate team
- Every player has 0 experience points
Your system must support the following operations:
join(x, y):
- Merge the teams containing players x and y.
- If both players already belong to the same team, no changes are made.
add(x, v): Add v experience points to every player in the team containing player x.
get(x) : Return the current experience points of player x.
A team can only be merged with another team. Teams can never be split, and a player can never leave their current team.
Return the result of every get operation in the order they appear.
Example 1:
Input: n = 3, calls = ["Experience", "add", "join", "add", "get", "get", "get"], params = [[3], [1, 100], [1, 3], [1, 50], [1], [2], [3]]
Output: [150, 0, 50]
Explanation:
- add(1, 100) adds 100 experience to player 1
- join(1, 3) merges the teams of players 1 and 3
- add(1, 50) adds 50 experience to every player in that merged team
- get(1) returns 150
- get(2) returns 0
- get(3) returns 50
Example 2:
Input: n = 5, calls = ["Experience", "join", "add", "join", "get", "get", "add", "get"], params = [[5], [2, 4], [2, 30], [1, 2], [1], [4], [5, 10], [5]]
Output: [30, 30, 10]
Explanation:
- join(2, 4) merges the teams of players 2 and 4
- add(2, 30) gives 30 experience to both players 2 and 4
- join(1, 2) merges player 1 into that team
- get(1) returns 30
- get(4) returns 30
- add(5, 10) gives 10 experience to player 5
- get(5) returns 10
Now Your Turn!
Pick the correct output for the given inputInput: n = 4, calls = ["Experience", "add", "join", "get"], params = [[4], [3, 25], [1, 3], [1]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 2 * 105
- 1 <= number of operations <= 2 * 105
- calls[0] == "Experience" and params[i] = [n]
- calls[i] == "join" and params[i] = [x, y]
- calls[i] == "add" and params[i] = [x, v]
- calls[i] == "get" and params[i] = [x]
- 1 <= x, y <= n
- 1 <= v <= 100