scala solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Codeor
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```scala []
object Solution {
  def maxScore(grid: List[List[Int]]): Int = {
    val n = grid.size
    val m = grid.head.size
    val f = Array.fill(n, m)(Int.MaxValue)
    var res = Int.MinValue
    for{i <- 0 until n
        j <- 0 until m}{
      var min = Int.MaxValue
      if(i > 0) min = Math.min(min, f(i - 1)(j))
      if(j > 0) min = Math.min(min, f(i)(j - 1))
      res = Math.max(res, grid(i)(j) - min)
      f(i)(j) = Math.min(min, grid(i)(j))
    }
    res
  }
}
```
or
```
  def maxScore(grid: List[List[Int]]): Int = {
    val n = grid.size
    val m = grid.head.size

    def helper(i: Int, j: Int, f: Array[Array[Int]], res: Int): Int = {
      if (i >= n) res
      else if (j >= m) helper(i + 1, 0, f, res)
      else {
        val min = (if (i > 0) Math.min(f(i - 1)(j), Int.MaxValue) else Int.MaxValue) min
          (if (j > 0) Math.min(f(i)(j - 1), Int.MaxValue) else Int.MaxValue)
        val newRes = Math.max(res, grid(i)(j) - min)
        f(i)(j) = Math.min(min, grid(i)(j))
        helper(i, j + 1, f, newRes)
      }
    }

    val f = Array.fill(n, m)(Int.MaxValue)
    helper(0, 0, f, Int.MinValue)
  }
```
['Scala']