盛最多水的容器

给定一个长度为 n 的整数列表 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。找出其中的两条线,使得它们与 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))
相关推荐
GinoWi40 分钟前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee1 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay1 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤1 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean2 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽3 小时前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户60648767188964 小时前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听4 小时前
RAG深入学习之Chunk
前端·人工智能·python
用户8356290780516 小时前
自动化文档处理:Python 批量提取 PDF 图片
后端·python
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc