3032. Maximize Value of Function in a Ball Passing Game
My accepted Python solution to LeetCode problem 3032, Maximize Value of Function in a Ball Passing Game, running in 6328ms.
- Difficulty: Hard
- Python
- Runtime 6328ms
- Memory 223.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 6328ms, memory 223.3MB, accepted 2026-01-02.
class Solution:
def getMaxFunctionValue(self, receiver: List[int], k: int) -> int:
n = len(receiver)
LOG = k.bit_length()
jump = [[0] * LOG for _ in range(n)]
sums = [[0] * LOG for _ in range(n)]
for i in range(n):
jump[i][0] = receiver[i]
sums[i][0] = receiver[i]
for j in range(1, LOG):
for i in range(n):
mid = jump[i][j-1]
jump[i][j] = jump[mid][j-1]
sums[i][j] = sums[i][j-1] + sums[mid][j-1]
result = 0
for start in range(n):
total = start
curr = start
for j in range(LOG):
if k & (1 << j):
total += sums[curr][j]
curr = jump[curr][j]
result = max(result, total)
return result