Problem · Array
Maximum Rocks on a North-or-East Grid Path
Learn this problemProblem statement
You are given a rectangular grid rocks, where rocks[row][column] is the number of rocks available in one city. Rows are ordered from south to north, and columns are ordered from west to east.
Start at the southwestern city [0, 0] and finish at the northeastern city [rows - 1, columns - 1]. From each city, you may move one row north or one column east.
Collect all rocks in every visited city, including the start and destination. Return the maximum number of rocks that can be collected.
Function
maximumCollectedRocks(rocks: int[][]) → longExamples
Example 1
rocks = [[5,1,2],[2,10,1],[1,1,20]]return = 38One optimal path visits values 5, 2, 10, 1, 20, for a total of 38.
Example 2
rocks = [[4,2,7]]return = 13With one row, the only path moves east through all three cities and collects 4 + 2 + 7 = 13.
Constraints
1 <= rocks.length <= 5001 <= rocks[row].length <= 500- Every row has the same length.
0 <= rocks[row][column] <= 10^9- The result fits in a signed 64-bit integer.