Track Previous Min
The path in the grid does not matter; the result is the difference between the first and last element.\n\nSo, we track the previously seen minimum element (to t
The path in the grid does not matter; the result is the difference between the first and last element.\n\nSo, we track the previously seen minimum element (to the left or above) of the current cell `g[i][j]` in `prev_min[j]`. \n\n**C++**\n```cpp\nint maxScore(vector<vector<int>>& g) {\n    int m = g.size(), n = g[0].size(), res = INT_MIN;\n    vector<int> min_j(n, INT_MAX);\n    for (int i = 0; i < m; ++i)\n        for (int j = 0; j < n; ++j) {\n            if (j > 0)\n                min_j[j] = min(min_j[j], min_j[j - 1]);\n            res = max(res, g[i][j] - min_j[j]);\n            min_j[j] = min(min_j[j], g[i][j]);\n        }\n    return res;\n}\n```
['C']