Problem · Array

Maximum Rocks on a North-or-East Grid Path

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem 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[][]) → long

Examples

Example 1

rocks = [[5,1,2],[2,10,1],[1,1,20]]return = 38

One optimal path visits values 5, 2, 10, 1, 20, for a total of 38.

Example 2

rocks = [[4,2,7]]return = 13

With one row, the only path moves east through all three cities and collects 4 + 2 + 7 = 13.

Constraints

  • 1 <= rocks.length <= 500
  • 1 <= 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.

More Goldman Sachs problems

drafts saved locally
public long maximumCollectedRocks(int[][] rocks) {
    // Write your code here.
}
rocks[[5,1,2],[2,10,1],[1,1,20]]
expected38
checking account