Container With Most Water

作者: d1497e8e780a | 来源:发表于2019-01-27 18:03 被阅读8次

Swift 4.2

class Solution {
  func maxArea(_ height: [Int]) -> Int {
    if height.count < 2 {
      return 0
    }
    var area = 0
    var leftIndex = 0
    var rightIndex = height.count - 1
    while (rightIndex - leftIndex) > 0 {
      let actualHeight = min(height[leftIndex],height[rightIndex])
      area = max(area, (rightIndex - leftIndex) * actualHeight)
      while height[leftIndex] <= actualHeight, leftIndex < rightIndex {
        leftIndex += 1
      }
      while height[rightIndex] <= actualHeight,leftIndex < rightIndex {
        rightIndex -= 1
      }
    }
    return area
  }

}

相关文章

网友评论

    本文标题:Container With Most Water

    本文链接:https://www.haomeiwen.com/subject/bsknjqtx.html