Skip to main content

Maximum Path Intersection Sum in a Grid - Solution & Explanation

Practice this problem

Problem Statement

You are given an m x n integer matrix grid.

Two players move across the grid:

  • Player 1 starts at the top-left cell (0, 0) and can move only right or down. Their destination is the bottom-right cell (m - 1, n - 1).
  • Player 2 starts at the bottom-left cell (m - 1, 0) and can move only right or up. Their destination is the top-right cell (0, n - 1).

Each player must choose a valid path from their respective starting cell to their destination.

A cell is called shared if it belongs to both chosen paths.

Return an integer denoting the maximum possible sum of values of all shared cells.

 

Example 1:

​​​​​​​​​​​​​​​​​​​​​

Input: grid = [[1,2,0,-3],[1,-2,1,0],[-4,2,-1,3],[3,-3,3,-2],[-1,-5,0,1]]

Output: 4

Explanation:

The diagram shows one optimal choice of paths.
  • Player 1 follows the red/purple path from the top-left cell to the bottom-right cell:
    • (0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3) → (3, 3) → (4, 3)
  • Player 2 follows the blue/purple path from the bottom-left cell to the top-right cell:
    • (4, 0) → (4, 1) → (3, 1) → (2, 1) → (2, 2) → (2, 3) → (1, 3) → (0, 3)
  • The shared cells are (2, 1), (2, 2), and (2, 3).
  • The sum is 2 + (-1) + 3 = 4, which is the maximum possible sum.

Example 2:

Input: grid = [[4,-2,-3],[-1,-3,-1],[-4,2,-1]]

Output: 3

Explanation:

One optimal pair of paths is shown in the diagram.

  • Player 1 follows the red/purple path:
    • (0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)
  • Player 2 follows the blue/purple path:
    • (2, 0) → (1, 0) → (0, 0) → (0, 1) → (0, 2)
  • The shared cells are (0, 0) and (1, 0).
  • The sum is 4 + (-1) = 3, which is the maximum possible.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 2 <= m, n <= 1000
  • 4 <= m * n <= 5 * 105
  • -100 <= grid[i][j] <= 100

Approach Overview

Problem Overview: You are given a grid of integers. Two optimal paths travel through the grid and intersect at some cell. The task is to determine the maximum possible sum contributed at the intersection when both paths are chosen optimally under standard grid movement rules.

Approach 1: Brute Force Path Enumeration (Exponential Time, Exponential Space)

The most direct strategy is to enumerate every possible path from the start cell to the destination and record the sum of values along each path. Then compare pairs of paths and compute the sum of cells where they intersect. This works conceptually but quickly becomes infeasible because the number of monotonic paths in an m x n grid grows combinatorially. Time complexity is roughly O(2^(m+n)) and space complexity is also exponential due to storing paths. This approach mainly helps reason about how intersections affect the total score.

Approach 2: Dynamic Programming with Four Directional Passes (O(m*n) Time, O(m*n) Space)

A practical solution precomputes the best path sums reaching every cell from different corners of the grid. Run four dynamic programming passes: from top-left, top-right, bottom-left, and bottom-right. Each DP table stores the maximum sum achievable when reaching that cell using valid moves. Once these tables are built, treat every cell as a potential intersection. Combine the contributions from the four directions to simulate two optimal paths crossing at that cell. The key idea is that the best intersection must combine optimal prefixes and suffixes of paths. This technique avoids enumerating paths and reduces the complexity to O(m*n) time and O(m*n) space using dynamic programming over a grid structure.

Approach 3: Space Optimized DP (O(m*n) Time, O(n) Space)

If memory becomes a constraint, the DP passes can be compressed to rolling rows or columns. Instead of storing the full DP table, maintain only the previous row or column during computation. The logic for combining intersection candidates stays the same, but intermediate states are reused as the grid is scanned. This reduces space complexity to O(n) while keeping the time complexity at O(m*n). The tradeoff is slightly more complex implementation and reduced readability compared with the full-table DP.

Recommended for interviews: The four-direction dynamic programming approach is the expected solution. Starting with the brute-force reasoning shows understanding of the search space, but recognizing that optimal substructure allows DP preprocessing demonstrates stronger problem-solving ability. Interviewers typically expect the O(m*n) DP solution using multiple passes over the grid.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Path EnumerationO(2^(m+n))O(2^(m+n))Conceptual understanding or very small grids
Dynamic Programming with Four PassesO(m*n)O(m*n)General case and interview solution
Space Optimized DPO(m*n)O(n)Large grids where memory usage matters

Video Solution

Leetcode 3938 | Maximum Path Intersection Sum in a Grid | Leetcode biweekly contest 183 • CodeWithMeGuys • 855 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Maximum Path Intersection Sum in a Grid easy or hard?
The problem is generally classified as Medium difficulty. The challenge comes from recognizing that two intersecting paths can be evaluated using multiple dynamic programming passes rather than enumerating all possible paths.
Maximum Path Intersection Sum in a Grid Python/Java solution
Most implementations use dynamic programming arrays to store prefix path sums from each corner of the grid. Python solutions typically use lists of lists for the DP tables, while Java solutions use 2D integer arrays. Both achieve O(m*n) time complexity.
How to solve Maximum Path Intersection Sum in a Grid in O(m*n)?
Precompute maximum path sums from all four grid corners using dynamic programming. For each cell, combine contributions from the four DP tables to simulate two optimal paths intersecting there. Evaluating all intersection candidates yields the maximum intersection sum in O(m*n) time.
What is the best approach for Maximum Path Intersection Sum in a Grid?
The most efficient solution uses dynamic programming with four directional passes across the grid. Compute the best path sums from each corner (top-left, top-right, bottom-left, bottom-right) and treat every cell as a potential intersection. Combining these precomputed values gives the optimal intersection sum in O(m*n) time.
Is Maximum Path Intersection Sum in a Grid asked at Google/Amazon/Meta?
Grid dynamic programming problems with path intersections frequently appear in interviews at companies like Amazon, Google, and Meta. Variants often test multi-source DP, path optimization, or problems similar to Cherry Pickup and two-agent grid traversal.
What data structure is used in Maximum Path Intersection Sum in a Grid?
The core data structure is a 2D dynamic programming table that stores the maximum path sum reaching each grid cell from a specific direction. Multiple DP tables are used to combine prefix and suffix path values around a potential intersection point.
What is the time complexity of Maximum Path Intersection Sum in a Grid?
The optimal dynamic programming solution runs in O(m*n) time because each cell in the grid is processed a constant number of times across four DP passes. The space complexity is O(m*n) when storing full DP tables, or O(n) with rolling array optimization.

Ready to solve this problem?

Practice Maximum Path Intersection Sum in a Grid with our built-in code editor and test cases.

Practice on FleetCode