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 can start at grid[3][0] = 2 and move to grid[2][2] = 14 for a score of 14 - 2 = 12. However, the optimal path is from grid[0][3] = 3 to grid[2][2] = 14 for a score of 14 - 3 = 11, but actually the best is from grid[3][0] = 2 to grid[1][0] = 8, then to grid[2][2] = 14. Wait, we can only make one move, so it's simply the maximum of (destination - source) for any valid pair. The maximum difference is 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
Grid Score MaximizationBuy Low (Min)Sell HighMaximum Profit = Sell High - Buy LowAlgorithm StateMin Price Seen: $5Current Price: $15Max Profit: $10✓ New Best!
Understanding the Visualization
1
Market Analysis
Scan through all available stock prices in chronological order
2
Track Best Buy Price
Keep track of the lowest price seen so far (best buying opportunity)
3
Calculate Profit
For each current price, calculate profit if we had bought at the best previous price
4
Maximize Returns
Keep track of the maximum profit possible across all opportunities
Key Takeaway
🎯 Key Insight: We don't need to check every possible pair. By maintaining the minimum value seen so far, we can calculate the optimal score for each position in a single pass!
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