Maximum Difference Score in a Grid - Problem
Grid Navigation Challenge: You're given an
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
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.
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 - c1Goal: 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code