Idiomatic Scala solution O(N)


#1
  def maxArea(A: Array[Int]): Int = {

    def loop(start: Int, end: Int, area: Int): Int = {
      if (start > end) area
      else {
        val nArea = (end - start) * Math.min(A(start), A(end))
        if (A(start) < A(end)) loop(start + 1, end, Math.max(area, nArea))
        else loop(start, end - 1, Math.max(area, nArea))
      }
    }

    loop(0, A.length - 1, 0)
  }