leetcode面试准备:Container With Most Water

1 题目

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.

接口: public int maxArea(int[] height);

2 思路

题意

在二维坐标系中,(i, ai) 表示 从 (i, 0) 到 (i, ai) 的一条线段,任意两条这样的线段和 x 轴组成一个木桶,找出能够盛水最多的木桶,返回其容积。

解法

两层 for 循环的暴力法会超时。

有没有 O(n) 的解法?

答案是有,用两个指针从两端开始向中间靠拢,如果左端线段短于右端,那么左端右移,反之右端左移,知道左右两端移到中间重合,记录这个过程中每一次组成木桶的容积,返回其中最大的。(想想这样为何合理?)

把实例{ 4, 6, 2, 6, 7, 11, 2 }走一遍,可以明白。

复杂度: Time:O(n) Space:O(1)

3 代码

	/*
* 两边夹逼
* Time:O(n) Space:O(1)
*/
public int maxArea(int[] height) {
int len = height.length;
int left = 0, right = len - 1;
int res = 0;
while (left < right) {
int local = Math.min(height[left], height[right]) * (right - left);
res = Math.max(res, local);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return res;
}

4 总结

Two Pointer思想

leetcode面试准备:Container With Most Water的更多相关文章

  1. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  2. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. LeetCode(11)题解: Container With Most Water

    https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...

  4. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  5. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  6. leetcode problem 11 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  7. LeetCode OJ 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  8. Leetcode Array 11 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  9. 【LeetCode】011 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

随机推荐

  1. w3c 学习html DOM

    什么是DOM? DOM是W3C标准,定义了访问HTML 和 XML文档的标准 W3C 文档对象模型(DOM)是中立于平台和语言接口,它允许程序动态的访问和更新文档的内容.结构和样式. W3C DOM ...

  2. div置于页面底部

    一直对于页面置底有一些困惑,下面这个例子不知道能不能解决 <!DOCTYPE html> <html lang="en"> <head> < ...

  3. jQuery Validate验证框架使用

    jQuery Validate使用前的准备,需要下载相应js包括:1.jquery.validate.min.js.2.additional-methods.min.js. 当然必不可少的js jQu ...

  4. 调试环境部署续:vs远程调试

    原文http://www.bitscn.com/weixin/464994.html 第一步  IIS的配置 进入iis,点击网址,选择你的网站,在窗口的右边编辑网站中点击绑定,如图所示. 进入网站绑 ...

  5. Oracle start with.connect by prior子句实现递归查询

    Oracle中的select语句可以用start with...connect by prior子句实现递归查询,connect by 是结构化查询中用到的,其基本语法是: select ... fr ...

  6. oracle job interval·相关事例

    描述 Interval参数值 每天运行一次 'SYSDATE + 1' 每小时运行一次 'SYSDATE + 1/24' 每10分钟运行一次 'SYSDATE + 10/(60*24)' 每30秒运行 ...

  7. OC - 11.使用Quartz2D剪裁图片并保存

    实现效果 操作步骤 绘制一个矩形框,弹出一个alertView,提示是否保存图片 点击"是",将图片保存到相册 在相册中查看保存的图片 效果图 实现思路 在控制器的view上添加一 ...

  8. js实现一个砖头在页面拖拉效果

    用js实现一个砖头在页面,但鼠标点击拖动时,砖头在页面上形成拖拉效果: 刚开始时: 鼠标点击拖动后: 实现代码: <html>   <head>       <meta ...

  9. js hashMap

    /** * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, ...

  10. 16_MyBatis中期小结

    [MyBatis是什么] MyBatis是一个持久层框架,Mybatis是一个不完全的ORM框架,SQL语句需要程序员自己去编写,但是MyBatis也有映射(输入参数映射.输出结果映射). MyBat ...