Maximum Game Board Score
Learn this problemProblem statement
You are given a game board represented by an integer array gameVal. The spaces are arranged in a straight line, and each value is the number of points on that space.
Choose any space as the starting position. Add the points on that space, then repeatedly jump exactly k spaces to the right and add the points on every space you land on. The game ends when the next jump would move past the final space.
Return the maximum total score obtainable by choosing the best starting position.
Function
maxGameScore(gameVal: int[], k: int) → intExamples
Example 1
gameVal = [2,-3,4,6,1]k = 2return = 7Starting at index 0 visits values 2, 4, and 1, producing 2 + 4 + 1 = 7. No other starting position produces a larger total.
Example 2
gameVal = [-5,-2,-7]k = 1return = -7Every selected starting position must continue jumping until it leaves the board. Starting at the final space produces -7, which is greater than the totals -14 and -9 from the earlier positions.
Constraints
1 <= gameVal.length <= 10^6-10^3 <= gameVal[i] <= 10^31 <= k < gameVal.length