Kotlin O(nm) time O(m) Space
Intuition\nThe maximum score for each cell is the difference between the cell\'s value and the minimum of the part of the matrix that is above and to the left o
# Intuition\nThe maximum score for each cell is the difference between the cell\'s value and the minimum of the part of the matrix that is above and to the left of that cell.\n\n# Approach\nIn the array `m` we will store and maintain the minimum of the matrix up to and including the corresponding column.\n\n# Complexity\n- Time complexity:\nO(nm)\n\n- Space complexity:\nO(m)\n\n# Code\n```kotlin []\nclass Solution {\n    fun maxScore(grid: List<List<Int>>): Int {\n        //m[j] \u2014 minimum value in the first j+1 columns\n        var m = IntArray(grid[0].size){Int.MAX_VALUE} \n        var r = Int.MIN_VALUE //r for result\n        for (i in 0..grid.lastIndex)\n            for (j in 0..grid[0].lastIndex)\n                //update max, the third argument is needed because the current minimum has not yet been updated\n                r = maxOf(r, grid[i][j] - m[j], grid[i][j] - if (j == 0) Int.MIN_VALUE else m[j-1])\n                    .also{ m[j] = minOf(m[j], if (j == 0) Int.MAX_VALUE else m[j-1], grid[i][j]) } //update min\n        return r\n    }\n}\n```
['Kotlin']