150强访谈简单

装最多水的容器

“装有最多水的容器”问题的详细指南和 Python 实现。

问题陈述

简单

给您一个长度为 n 的整数数组 height。绘制了 n 条垂直线,使得第 i 线的两个端点是 (i, 0)(i, height[i])

找到与 x 轴一起形成容器的两条线,使得该容器包含最多的水。

返回容器可以储存的最大水量。

请注意,您不能倾斜容器。

编写一个函数 maxArea(height: List[int]) -> int

约束条件
  • n == len(height)
  • 2 <= n <= 10^5
  • 0 <= height[i] <= 10^4

示例

Example 1
Input
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output
49
Explanation

The max area is between lines at index 1 (height 8) and index 8 (height 7). Area = min(8, 7) * (8 - 1) = 7 * 7 = 49.

Example 2
Input
height = [1, 1]
Output
1
Explanation

Area = min(1, 1) * (1 - 0) = 1.

Need a Hint?
考虑使用特定于两指针的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。