码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 11. Container With Most Water

时间:2019-11-02 14:09:55      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:inter   class   ram   col   while   fun   最大   style   container   

装水最多的容器。算是two pointer类型的题里面比较弱智的一道吧。提议是用数组表示出一堆木桶的高度,问如果用其中两个木桶做左右的墙壁,能储存的水量最大是多少。搞懂题意,代码基本也就出来了。

时间O(n)

空间O(1)

 1 /**
 2  * @param {number[]} height
 3  * @return {number}
 4  */
 5 var maxArea = function (height) {
 6     let res = 0;
 7     let left = 0;
 8     let right = height.length - 1;
 9     while (left < right) {
10         res = Math.max(res, Math.min(height[left], height[right]) * (right - left));
11         if (height[left] < height[right]) {
12             left++;
13         } else {
14             right--;
15         }
16     }
17     return res;
18 };

 

[LeetCode] 11. Container With Most Water

标签:inter   class   ram   col   while   fun   最大   style   container   

原文地址:https://www.cnblogs.com/aaronliu1991/p/11781368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!