---
# System prepended metadata

title: Path With Maximum Score

---

# Path With Maximum Score

https://leetcode.com/discuss/interview-question/383669/

```python=
def maxScorePath(grid):
    if not grid:
        return 0
    if len(grid) == 1 and len(grid[0]) == 2:
        return 0
    if len(grid) == 2 and len(grid[0]) == 1:
        return 0
    
    for j in range(2, len(grid[0])):
        if j == len(grid[0]) - 1:
            if len(grid) == 1:
                grid[0][j] = grid[0][j-1]
            else:
                grid[0][j] = min(grid[0][j-1],grid[0][j])
        else:
            grid[0][j] = min(grid[0][j-1],grid[0][j])
    
    for i in range(2, len(grid)):
        if i == len(grid) - 1:
            if len(grid[0]) == 1:
                grid[i][0] = grid[i-1][0]
            else:
                grid[i][0] = min(grid[i-1][0],grid[i][0])
        else:
            grid[i][0] = min(grid[i-1][0],grid[i][0])
    
    for i in range(1, len(grid)):
        for j in range(1, len(grid[0])):
            if i == len(grid)-1 and j == len(grid[0])-1:
                grid[i][j] = max(grid[i-1][j],grid[i][j-1])
            else:
                grid[i][j] = max(min(grid[i-1][j],grid[i][j]),min(grid[i][j-1],grid[i][j]))
                
    return grid[len(grid)-1][len(grid[0])-1]
```

## some test cases
https://leetcode.com/playground/KCvxWBQp