You are given an m x n integer matrix grid.
Two players move across the grid:
(0, 0) and can move only right or down. Their destination is the bottom-right cell (m - 1, n - 1).(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.(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3) → (3, 3) → (4, 3)(4, 0) → (4, 1) → (3, 1) → (2, 1) → (2, 2) → (2, 3) → (1, 3) → (0, 3)(2, 1), (2, 2), and (2, 3).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.
(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)(2, 0) → (1, 0) → (0, 0) → (0, 1) → (0, 2)(0, 0) and (1, 0).4 + (-1) = 3, which is the maximum possible.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 5 * 105-100 <= grid[i][j] <= 100Problem 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| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Path Enumeration | O(2^(m+n)) | O(2^(m+n)) | Conceptual understanding or very small grids |
| Dynamic Programming with Four Passes | O(m*n) | O(m*n) | General case and interview solution |
| Space Optimized DP | O(m*n) | O(n) | Large grids where memory usage matters |
Practice Maximum Path Intersection Sum in a Grid with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor