Maximum Path Intersection Sum in a Grid Problem
#3938
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3938.
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.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 5 * 10^5-100 <= grid[i][j] <= 100
Solution
Approach: Best Segment with Kadane
One path goes down-right and the other goes up-right, so they cross, and the cells they share always form a straight segment: part of one row, or part of one column.
- A segment of two or more cells can always be realized exactly, by letting the paths enter and leave it from different sides. So the best such segment is the best subarray with at least two cells in any row or any column. For each row (and column) we run Kadane's algorithm and, for every cell, combine it with the best sum ending at the previous cell.
- A single cell needs extra care, because both paths pass through it and must use different neighbors. A cell with all four neighbors always works. For a cell on the border we try every way to enter and leave it: path 1 enters from above or the left and leaves down or right; path 2 enters from below or the left and leaves up or right. The cell is valid if some choice avoids sharing the cell above, the cell below, the left neighbor and the right neighbor. The start and end cells have fewer options.
The answer is the largest value among these candidates.
Complexity: O(mn) time and O(1) extra space.
class Solution:
def maxScore(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = -float("inf")
# The shared cells always form one straight segment. A segment with
# two or more cells can always be realized, so take the best one.
for row in grid:
end = row[0]
for j in range(1, n):
ans = max(ans, end + row[j])
end = max(row[j], end + row[j])
for j in range(n):
end = grid[0][j]
for i in range(1, m):
ans = max(ans, end + grid[i][j])
end = max(grid[i][j], end + grid[i][j])
# A single shared cell needs the two paths to enter and leave it
# through different neighbors.
def can_share(r: int, c: int) -> bool:
up, down, left, right = r > 0, r < m - 1, c > 0, c < n - 1
in1 = [""] if r == 0 and c == 0 else ["U"] * up + ["L"] * left
out1 = [""] if r == m - 1 and c == n - 1 else ["D"] * down + ["R"] * right
in2 = [""] if r == m - 1 and c == 0 else ["D"] * down + ["L"] * left
out2 = [""] if r == 0 and c == n - 1 else ["U"] * up + ["R"] * right
for a in in1:
for b in out1:
for c2 in in2:
for d in out2:
if (
(a == "U" and d == "U")
or (b == "D" and c2 == "D")
or (a == "L" and c2 == "L")
or (b == "R" and d == "R")
):
continue
return True
return False
for r in range(m):
for c in range(n):
if grid[r][c] > ans and (0 < r < m - 1 and 0 < c < n - 1 or can_share(r, c)):
ans = grid[r][c]
return ans