[Java/C++/Python] DP, Minimum on Top-Left
Intuition\nMove from c1 to ck with path c1,c2,..,ck,\nres = (c2 - c1) + (c3 - c2) + .... = ck - c1\n\n\n# Explanation\nTo calculate the best score ending at a c
# **Intuition**\nMove from `c1` to `ck` with path `c1,c2,..,ck`,\n`res = (c2 - c1) + (c3 - c2) + .... = ck - c1`\n<br>\n\n# **Explanation**\nTo calculate the best score ending at a cell `ck`,\njust need to find out the minimum `c1` on its top left area.\n\nTo do this, we can define `dp[i][j]` as the minimum in `A[i][j]` and its top-left.\n`dp[i][j] = min(A[i][j], dp[i-1][j], dp[i][j-1]))`\n\nInstead of create an array for DP,\nwe can also do it in-palce of `A`,\nto same some codes and spaces.\n<br>\n\n# **Complexity**\nTime `O(mn)`\nSpace `O(1)`\n<br>\n\n**Java**\n```java\n    public int maxScore(List<List<Integer>> A) {\n        int res = -1000_000, m = A.size(), n = A.get(0).size();\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                int pre = Math.min(\n                    i > 0 ? A.get(i - 1).get(j) : 1000_000,\n                    j > 0 ? A.get(i).get(j - 1) : 1000_000\n                );\n                res = Math.max(res, A.get(i).get(j) - pre);\n                if (pre < A.get(i).get(j)) {\n                    A.get(i).set(j, pre);\n                }\n            }\n        }\n        return res;\n    }\n```\n**C++**\n```cpp\n    int maxScore(vector<vector<int>>& A) {\n        int res = -1e6, m = A.size(), n = A[0].size();\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                int pre = min(i > 0 ? A[i - 1][j] : 1e6, j > 0 ? A[i][j - 1] : 1e6);\n                res = max(res, A[i][j] - pre);\n                A[i][j] = min(A[i][j], pre);\n            }\n        }\n        return res;\n    }\n```\n\n**Python**\n```py\n    def maxScore(self, A: List[List[int]]) -> int:\n        res, m, n = -inf, len(A), len(A[0])\n        for i in range(m):\n            for j in range(n):\n                pre = min(A[i-1][j] if i else inf, A[i][j-1] if j else inf)\n                res = max(res, A[i][j] - pre)\n                A[i][j] = min(A[i][j], pre)\n        return res\n```\n\n<br>\n\n# More DP problems\nHere is small DP problem list.\nGood luck and have fun.\n- 3148. [Maximum Difference Score in a Grid](https://leetcode.com/problems/maximum-difference-score-in-a-grid/discuss/5145704/JavaC%2B%2BPython-Minimum-on-Top-Left)\n- 3147. [Taking Maximum Energy From the Mystic Dungeon](https://leetcode.com/problems/taking-maximum-energy-from-the-mystic-dungeon/discuss/5145723/JavaC%2B%2BPython-DP)\n- 2920. [Maximum Points After Collecting Coins From All Nodes](https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes/discuss/4220643/JavaC%2B%2BPython-DP)\n- 2919. [Minimum Increment Operations to Make Array Beautiful](https://leetcode.com/problems/minimum-increment-operations-to-make-array-beautiful/discuss/4220717/JavaC%2B%2BPython-Easy-and-Concise-DP-O(1)-Space)\n- 2896. [Apply Operations to Make Two Strings Equal](https://leetcode.com/problems/apply-operations-to-make-two-strings-equal/discuss/4144041/JavaC%2B%2BPython-DP-One-Pass-O(1)-Space)\n- 2830. [Maximize the Profit as the Salesman](https://leetcode.com/problems/maximize-the-profit-as-the-salesman/discuss/3934188/JavaC%2B%2BPython-DP)\n- 2008. [Maximum Earnings From Taxi](https://leetcode.com/problems/maximum-earnings-from-taxi/discuss/1470941/JavaC%2B%2BPython-DP-solution)\n- 1751. [Maximum Number of Events That Can Be Attended II](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended-ii/discuss/1052581/Python-DP)\n- 1235. [Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/discuss/409009/Python-DP-Solution)\n
['C', 'Python', 'Java']