code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

011 Container With Most Water

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const containerWithMostWater = nums => {
  let result = 0
  let left = 0
  let right = nums.length - 1
  while (left < right) {
    const minLine = Math.min(nums[left], nums[right])
    const area = minLine * (right - left)
    result = Math.max(result, area)
    if (nums[left] < nums[right]) {
      left += 1
    } else {
      right -= 1
    }
  }
  return result
}

test('containerWithMostWater', () => {
  expect(containerWithMostWater([1, 8, 6, 2, 5, 4, 8, 3, 7])).toEqual(49)
})

Created 2021-01-01T08:10:58+05:30 · Edit