盛最多水的容器

给定一个长度为 n 的整数列表 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, heighti) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。

说明:你不能倾斜容器。

示例1:

输入:1,8,6,2,5,4,8,3,7

输出:49

解释:图中垂直线代表输入数组 1,8,6,2,5,4,8,3,7。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为7*7=49。

示例 2:

输入: 1,1

输出:1

解释:输入数组 1,1,在此情况下,容器能够容纳水的最大值为 1*1=1。

python 复制代码
def maxArea(height):
    i, j, res = 0, len(height) - 1, 0
    while i < j:
        if height[i] < height[j]:
            res = max(res, height[i] * (j - i))
            i += 1
        else:
            res = max(res, height[j] * (j - i))
            j -= 1
    return res

height = eval(input())
print(maxArea(height))
相关推荐
曲幽2 小时前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
装不满的克莱因瓶2 小时前
掌握 RNN 与 LSTM 模型结构
人工智能·python·rnn·深度学习·神经网络·ai·lstm
何以解忧,唯有..3 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
金銀銅鐵3 小时前
用 Tkinter 实现简单的猜数字游戏
后端·python
copyer_xyf3 小时前
Python 模块与包的导入导出
前端·后端·python
ice8130331814 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
copyer_xyf4 小时前
Python venv 虚拟环境
前端·后端·python
林爷万福5 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪
copyer_xyf5 小时前
Python 如何同时做很多事:进程、线程、协程
前端·后端·python
Full Stack Developme5 小时前
Spring Bean 依赖注入
python·spring·log4j