DSA Round: Maximum Score Grid Path
Problem statement
You are given a rectangular grid board of size N x M, where each cell contains an integer. A player starts from cell (0, p) in the top row. The objective is to reach the bottom row of the grid while maximizing the total score collected.
From a cell (i, j), the player may move to:
(i + 1, j - 1), diagonally left-down(i + 1, j), straight down(i + 1, j + 1), diagonally right-down(i + 2, j), using a special jump move
The player must always remain inside the grid boundaries. The special jump move (i + 2, j) may be used at most k times during the traversal.
The total score is equal to the sum of all visited cell values, including the starting cell and the final cell.
Complete the function maximizeGridPathScore. It should return a String[] with exactly three values:
result[0]: the maximum achievable score from the starting cell to any cell in the last row.result[1]: one path that produces the maximum score, formatted as coordinates joined by->, for example(0,1)->(1,1).result[2]: the number of distinct maximum-score paths modulo10^9 + 7.
If multiple maximum-score paths exist, return the lexicographically smallest coordinate sequence as the path in result[1]. Compare two paths by their coordinate pairs from left to right; the smaller row wins first, then the smaller column.
Overview
This problem is about maximizing score while moving through a rectangular integer grid. It has four allowed moves, including a limited special jump, and asks for the maximum score, one maximum-score path, and the count of maximum-score paths modulo 10^9 + 7.
Output rules
Return one maximum-score path; among ties, return the lexicographically smallest coordinate sequence.
Function
maximizeGridPathScore(board: int[][], p: int, k: int) → String[]Examples
Example 1
board = [[1,2,3,4],[5,6,1,2],[7,8,9,1],[3,2,5,6]]p = 1k = 1return = ["23","(0,1)->(1,1)->(2,2)->(3,3)","1"]One maximum-score path is (0,1)->(1,1)->(2,2)->(3,3).
- Visited values:
2 + 6 + 9 + 6 = 23. - The alternate valid jump path
(0,1)->(2,1)->(3,2)scores2 + 8 + 5 = 15, so it is not a maximum-score path. - There is exactly one maximum-score path for this example.
Constraints
boardis rectangular and contains integers.pis a valid column index in the top row.- The player may use the special jump at most
ktimes. - Return the maximum-path count modulo
10^9 + 7.