Class Solution
-
- All Implemented Interfaces:
public final class Solution2836 - Maximize Value of Function in a Ball Passing Game\.
Hard
You are given a 0-indexed integer array
receiverof lengthnand an integerk.There are
nplayers having a unique id in the range[0, n - 1]who will play a ball passing game, andreceiver[i]is the id of the player who receives passes from the player with idi. Players can pass to themselves, i.e.receiver[i]may be equal toi.You must choose one of the
nplayers as the starting player for the game, and the ball will be passed exactlyktimes starting from the chosen player.For a chosen starting player having id
x, we define a functionf(x)that denotes the sum ofxand the ids of all players who receive the ball during thekpasses, including repetitions. In other words, <code>f(x) = x + receiverx + receiver[receiverx] + ... + receiver<sup>(k)</sup>x</code>.Your task is to choose a starting player having id
xthat maximizes the value off(x).Return an integer denoting the maximum value of the function.
Note:
receivermay contain duplicates.Example 1:
Input: receiver = 2,0,1, k = 4
Output: 6
Explanation: The table above shows a simulation of the game starting with the player having id x = 2. From the table, f(2) is equal to 6. It can be shown that 6 is the maximum achievable value of the function. Hence, the output is 6.
Example 2:
Input: receiver = 1,1,1,2,3, k = 3
Output: 10
Explanation: The table above shows a simulation of the game starting with the player having id x = 4. From the table, f(4) is equal to 10. It can be shown that 10 is the maximum achievable value of the function. Hence, the output is 10.
Constraints:
<code>1 <= receiver.length == n <= 10<sup>5</sup></code>
0 <= receiver[i] <= n - 1<code>1 <= k <= 10<sup>10</sup></code>