盛最多水的容器

给定一个长度为 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))
相关推荐
王仔小奶11 小时前
慢SQL治理与性能调优:DBA如何用Shell/Python提升运维效率?
python·sql·dba
用户03321266636711 小时前
使用 Python 快速在 PowerPoint 中生成表格【教程】
python
夜晚回家11 小时前
Python 身份运算符详解 | is 与 == 到底差在哪
python
fengye20716111 小时前
cpython框架结构
python
用户912940468484211 小时前
大模型返回 JSON 后,先过这道 Python 校验
python
Dmz丶11 小时前
前端转 AI 100 天 Day 32:LLM 客户端工程化——重试、超时、降级、限流
python
曲径海盗11 小时前
【Python 学习第 39 天】for 循环
python·学习笔记·编程入门
万联WANFLOW12 小时前
Kimi K3.1 Pro 接口接入指南:模型选型、费用计算与零数据保留
java·spring boot·python·安全·spring·大模型
问心无愧051312 小时前
[护网杯 2018]easy_tornado
python·tornado
全栈弄潮儿13 小时前
Python实战第13期:模块与包
后端·python·编程语言