FastPrepMaximum Grid Path Sum in Exactly N Moves

Maximum Grid Path Sum in Exactly N Moves

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Start at [startRow,startCol]. Make exactly moves one-cell moves up, down, left, or right without leaving the grid. The start contributes zero; each entered cell contributes its value. Revisiting is allowed.

Return the maximum achievable sum.

Function

maximumPathSum(grid: int[][], startRow: int, startCol: int, moves: int) → int

Examples

Example 1

grid = [[2,3,4,6],[1,2,3,5],[3,4,0,5],[0,1,2,3]]startRow = 2startCol = 2moves = 2return = 10

Move right to 5 and back or between the two adjacent 5 cells for a total of 10.

Constraints

  • The grid is nonempty and rectangular.
  • 0 <= moves <= 1000.
  • A valid move sequence exists.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int maximumPathSum(int[][] grid, int startRow, int startCol, int moves) {
  // Write your code here.
}
grid[[2,3,4,6],[1,2,3,5],[3,4,0,5],[0,1,2,3]]
startRow2
startCol2
moves2
expected10
Checking account…