盛最多水的容器

给定一个长度为 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))
相关推荐
mit6.8247 分钟前
archived
后端·python·flask
船长@10 分钟前
FastAPI 快速上手:从零基础到实操入门
python·ai·fastapi
我要两颗404西柚1 小时前
2【python】:常用关键字,函数,方法
python
COOLMO研究AI1 小时前
Python 如何给 AI API 实现断路器模式:防止雪崩与保护本地服务
人工智能·python·php
zzzzzz3101 小时前
LLM 成本治理:从 token 账单到线上熔断,团队应该先做哪三件事?
人工智能·python·api
AC赳赳老秦1 小时前
网页公开附件自动采集:OpenClaw 批量下载页面内嵌 Word/Excel 附件,统一格式后结构化入库
java·python·word·php·excel·deepseek·openclaw
北斗落凡尘1 小时前
LangGraph 入门实战(3)
python·langchain
TAN-90°-2 小时前
Deep Learning for Computer Vision——Image Classification with Linear Classifiers
python·深度学习·算法·计算机视觉·线性回归
AINative软件工程2 小时前
LLM API 成本失控怎么办?工程师的实时异常检测指南
python
练习两年半的攻城狮2 小时前
LlamaIndex ResponseMode 深度解析
python·llamaindex