[062] LeetCode 11 演算法【Container With Most Water】盛水最多的容器

M
Leetcode 演算法教學
Oct 23, 2020

11. Container With Most Water (Medium)

Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

這題思路很簡單,使用雙指針去判斷,我們用雙層回圈也可以做到這件事情,題目是找出最大面積,假設現在兩個都是7的話他就會是7*7=49,我們先釐清一下一些定義:

1.只移動矮的,因為高的移動只會讓面積更小
2.是用最矮的長度去相乘

大家加油。

上一篇:[061] LeetCode 309演算法【Best Time to Buy and Sell Stock with Cooldown】 股票機器人擬人版

下一篇:[063] LeetCode 42 演算法【Trapping Rain Water】收集雨水

--

--