2836. Maximize Value of Function in a Ball Passing Game Hard
1/**
2 * [2836] Maximize Value of Function in a Ball Passing Game
3 *
4 * You are given an integer array receiver of length n and an integer k. n players are playing a ball-passing game.
5 * You choose the starting player, i. The game proceeds as follows: player i passes the ball to player receiver[i], who then passes it to receiver[receiver[i]], and so on, for k passes in total. The game's score is the sum of the indices of the players who touched the ball, including repetitions, i.e. i + receiver[i] + receiver[receiver[i]] + ... + receiver^(k)[i].
6 * Return the maximum possible score.
7 * Notes:
8 *
9 * receiver may contain duplicates.
10 * receiver[i] may be equal to i.
11 *
12 *
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">receiver = [2,0,1], k = 4</span>
16 * Output: <span class="example-io">6</span>
17 * Explanation:
18 * Starting with player i = 2 the initial score is 2:
19 * <table>
20 * <tbody>
21 * <tr>
22 * <th>Pass</th>
23 * <th>Sender Index</th>
24 * <th>Receiver Index</th>
25 * <th>Score</th>
26 * </tr>
27 * <tr>
28 * <td>1</td>
29 * <td>2</td>
30 * <td>1</td>
31 * <td>3</td>
32 * </tr>
33 * <tr>
34 * <td>2</td>
35 * <td>1</td>
36 * <td>0</td>
37 * <td>3</td>
38 * </tr>
39 * <tr>
40 * <td>3</td>
41 * <td>0</td>
42 * <td>2</td>
43 * <td>5</td>
44 * </tr>
45 * <tr>
46 * <td>4</td>
47 * <td>2</td>
48 * <td>1</td>
49 * <td>6</td>
50 * </tr>
51 * </tbody>
52 * </table>
53 * </div>
54 * <strong class="example">Example 2:
55 * <div class="example-block">
56 * Input: <span class="example-io">receiver = [1,1,1,2,3], k = 3</span>
57 * Output: <span class="example-io">10</span>
58 * Explanation:
59 * Starting with player i = 4 the initial score is 4:
60 * <table>
61 * <tbody>
62 * <tr>
63 * <th>Pass</th>
64 * <th>Sender Index</th>
65 * <th>Receiver Index</th>
66 * <th>Score</th>
67 * </tr>
68 * <tr>
69 * <td>1</td>
70 * <td>4</td>
71 * <td>3</td>
72 * <td>7</td>
73 * </tr>
74 * <tr>
75 * <td>2</td>
76 * <td>3</td>
77 * <td>2</td>
78 * <td>9</td>
79 * </tr>
80 * <tr>
81 * <td>3</td>
82 * <td>2</td>
83 * <td>1</td>
84 * <td>10</td>
85 * </tr>
86 * </tbody>
87 * </table>
88 * </div>
89 *
90 * Constraints:
91 *
92 * 1 <= receiver.length == n <= 10^5
93 * 0 <= receiver[i] <= n - 1
94 * 1 <= k <= 10^10
95 *
96 */
97pub struct Solution {}
98
99// problem: https://leetcode.com/problems/maximize-value-of-function-in-a-ball-passing-game/
100// discuss: https://leetcode.com/problems/maximize-value-of-function-in-a-ball-passing-game/discuss/?currentPage=1&orderBy=most_votes&query=
101
102// submission codes start here
103
104impl Solution {
105 pub fn get_max_function_value(receiver: Vec<i32>, k: i64) -> i64 {
106
107 }
108}
109
110// submission codes end
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn test_2836() {
118 }
119}
120
Back
© 2025 bowen.ge All Rights Reserved.