Container With Most Water

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array

S = [-1, 0, 1, 2, -1, -4]

A solution set is:

[ [-1, 0, 1], [-1, -1, 2] ]
/**
  *
  * @param height
  * @return
  */
def maxArea(height: Array[Int]):Int = {
  var i,j,lvl,vol: Int = 0
  j = height.length - 1
  while (i < j) {
    lvl = height(i) min height(j)
    vol = vol max lvl * (j-i)
    while (i <= j && height(i) <= lvl) i += 1
    while (j >= 0 && height(j) <= lvl) j -= 1
  }
  vol
}

maxArea(Array())
maxArea(Array(1,1))
maxArea(Array(1,1,5,2,3))
maxArea(Array(1,1,3,2,5))

Last updated