100% Fast Code | Simple Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea is to store the minimum element encountered in the top and left portion for ea
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is to store the minimum element encountered in the top and left portion for each position in the grid. Then, we calculate the final answer as the maximum difference between the stored minimum and the element at that position.\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int maxScore(List<List<Integer>> grid) {\n        int n = grid.size();\n        int m = grid.get(0).size();\n        int[][] dp = new int[n][m];\n        dp[0][0] = grid.get(0).get(0);\n        \n        for (int i = 1; i < n; i++) {\n            dp[i][0] = Math.min(dp[i - 1][0], grid.get(i).get(0));\n        }\n        \n        for (int i = 1; i < m; i++) {\n            dp[0][i] = Math.min(dp[0][i - 1], grid.get(0).get(i));\n        }\n        \n        for (int i = 1; i < n; i++) {\n            for (int j = 1; j < m; j++) {\n                dp[i][j] = Math.min(grid.get(i).get(j),Math.min(dp[i - 1][j], dp[i][j - 1]));\n            }\n        }\n\n        int ans = Integer.MIN_VALUE;\n        \n        for (int i = 1; i < n; i++) {\n            ans = Math.max(ans, grid.get(i).get(0) - dp[i - 1][0]);\n        }\n        \n        for (int i = 1; i < m; i++) {\n            ans = Math.max(ans, grid.get(0).get(i) - dp[0][i - 1]);\n        }\n        \n        for (int i = 1; i < n; i++) {\n            for (int j = 1; j < m; j++) {\n                ans = Math.max(ans, grid.get(i).get(j) - Math.min(dp[i - 1][j], dp[i][j - 1]));\n            }\n        }\n        \n        return ans;\n    }\n}\n\n```
['Array', 'Dynamic Programming', 'Java']