Ctrl K

Best Gain by Jumps

Medium
You are given a board board with m rows and n columns of positive numbers. A token moves only down or right, and a single move may skip over any number of cells: from (i, j) it can go to any (i', j) with i' > i or to any (i, j') with j' > j. A move from a cell holding c1 to a cell holding c2 scores c2 - c1 points, which may be negative. You may start on any cell, but you must make at least one move. Return the largest total score you can collect.
Example 1
board=[[6, 2, 8], [5, 1, 4], [9, 3, 7]] → 6

Start on the 1 (row 1, column 1), jump right to the 4 (+3), then down to the 7 (+3), for 6 in total. Jumping straight from 1 to 7 gives the same 6, and nothing beats it.

Example 2
board=[[10, 8], [7, 3]] → -2

Every jump lands on a smaller number. The least painful single move is 10 → 8, worth −2, and that is the maximum.

Example 3
board=[[4, 4], [4, 4]] → 0

All values are equal, so every move scores 0.

Constraints
  • 2 ≤ m, n ≤ 1000
  • 4 ≤ m * n ≤ 10⁵
  • 1 ≤ board[i][j] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWarehouse Cart Routes MediumCheapest Corner-to-Corner Route MediumLargest Solid Square MediumBest Spot for the Turret MediumSteps to the Nearest Zero Medium
loading Python…
Input
board=[[6, 2, 8], [5, 1, 4], [9, 3, 7]]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 79 tests on the server, hidden ones included.