题解 | #盛水最多的容器#

盛水最多的容器

https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47

class Solution:  
    def maxArea(self, height: List[int]) -> int:  
        if len(height) < 2:  
            return 0  
          
        left = 0  
        right = len(height) - 1  
        max_area = 0  
          
        while left < right:  
            # 计算当前左右指针所形成的容器的面积  
            area = min(height[left], height[right]) * (right - left)  
            # 更新最大面积  
            max_area = max(max_area, area)  
              
            # 移动指针,由于容器的面积取决于较短的那条边,因此我们将较短的边所对应的指针向内移动  
            if height[left] < height[right]:  
                left += 1  
            else:  
                right -= 1  
          
        return max_area

全部评论

相关推荐

07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务