Maximum Difference Score in a Grid - Problem
Grid Navigation Challenge: You're given an m × n matrix filled with positive integers. Your mission is to find the maximum score by making strategic moves through the grid.

Movement Rules:
• You can move from any cell to any other cell that is either below or to the right (not necessarily adjacent)
• You can start at any cell in the grid
• You must make at least one move

Scoring System:
The score for moving from cell with value c1 to cell with value c2 is c2 - c1

Goal: Return the maximum total score you can achieve across all possible paths.

Example: If you move from cell with value 5 to cell with value 8, you gain 3 points. If you then move to a cell with value 6, you lose 2 points, for a total score of 1.

Input & Output

example_1.py — Basic Grid
$ Input: grid = [[9,5,7,3], [8,9,6,1], [6,7,14,3], [2,5,3,1]]
Output: 9
💡 Note: We need to find the maximum value of (destination - source) where we can move from source to destination (only right or down). The optimal move is from grid[0][1] = 5 to grid[2][2] = 14, giving us a score of 14 - 5 = 9.
example_2.py — Small Grid
$ Input: grid = [[4,3,2], [3,2,1]]
Output: -1
💡 Note: Since we can only move right or down, and all values are decreasing in those directions, the best we can do is lose as little as possible. The minimum loss is 1 (from 2 to 1), so the maximum score is -1.
example_3.py — Single Row
$ Input: grid = [[1,5,3,9,2]]
Output: 8
💡 Note: We can move from the first cell (1) to the fourth cell (9) for a score of 9 - 1 = 8, which is the maximum possible.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 2 ≤ m, n ≤ 1000
  • 4 ≤ m × n ≤ 105
  • 1 ≤ grid[i][j] ≤ 105
  • You must make at least one move

Visualization

Tap to expand
Maximum Difference Score in a Grid INPUT 9 5 7 3 8 9 6 1 6 7 14 3 2 5 3 1 4x4 Matrix Move: right or down Start (5) End (14) grid = [[9,5,7,3], [8,9,6,1], [6,7,14,3], [2,5,3,1]] ALGORITHM STEPS 1 Track Min Prefix Store min value reachable from top-left to each cell 2 DP Transition minPrefix[i][j] = min of grid[i][j], top, left mins 3 Calculate Score For each cell: score = grid[i][j] - minPrefix 4 Find Maximum Track max score across all valid moves Best Path Found: Start at (3,1): value = 5 End at (2,2): value = 14 Score = 14 - 5 = 9 FINAL RESULT 9 5 7 3 8 9 6 1 6 7 14 3 2 5 3 1 OUTPUT 9 OK - Maximum Score Path: (3,1) --> (2,2) Score: 14 - 5 = 9 Key Insight: Use Dynamic Programming to track the minimum value reachable from the top-left corner to each cell. For each cell, the best score ending there is: current_value - min_prefix (minimum from cells that can reach it). Time Complexity: O(m*n) | Space Complexity: O(m*n) - can be optimized to O(n) with row-by-row processing. TutorialsPoint - Maximum Difference Score in a Grid | Optimal DP Solution
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.4K Views
Medium Frequency
~18 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen