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. hibernate篇章六--demo(0.准备工作)

    这个demo是学习自:学习不可已 这个章节我们先来说准备工作:Hibernate之第〇解之准备工作 首先: 1.新建java project--hibernate_demo_1: 2.新建src fo ...

  2. 第三篇:gradle 编译 Android app 概览

    引言:经过上两篇的论述,我们已经从代码到架构都简单的熟悉了一遍,理论上,只要知道android app的编译过程,我们大可以自己写一份用gradle编译app的插件,插件内将将整个流程用Task的依赖 ...

  3. IP-MAC绑定导致网络故障

    前段时间将一台服务器A的服务迁移至了另外一台服务器B,外网IP地址也顺带迁移过来了,结果网络出现了问题. 其中内网是畅通的,但是外网IP怎么都连不上另外一台路由C(B和C是在一个交换机下的,网段也相同 ...

  4. Visual C++ 打印编程技术-打印基础知识

    打印机介绍 1.打印术语 *: 1 英寸= 2.54 厘米(cm)= 25.4 毫米(mm) cpi (Characters Per Inch): 每英寸内所含的字符数,用来表示字符的大小.间距 cp ...

  5. input设置disabled,经过strus2提交到后台,后台取不到值

    页面中有多个name相同的input与后台action中一个属性对应,一直在好奇为什么会可以提交到后台呐,但是有时还报这个属性找不到对应的方法(多个name相同好像匹配的是数组,所以找不到), 但是我 ...

  6. iOS--NSNetService和NSNetServiceBrowser(Bonjour网络编程)

    Cocoa 网络框架: Cocoa 网络框架有三层,最底层的是基于 BSD socket库,然后是 Cocoa 中基于 C 的 CFNetwork,最上面一层是 Cocoa 中 Bonjour.通常我 ...

  7. window redis 安装配置

    1 下载 https://github.com/MSOpenTech/redis/releases 当前最新版本为 redis-2.8.21   下载的为zip包,下载连接为:https://gith ...

  8. JAVA中实现百分比

    import java.text.NumberFormat; public class TeachYou { public static void main(String[] args) { //这里 ...

  9. myeclipse、eclipse去掉无用的workSpace

    在 eclipse\configuration\.settings\org.eclipse.ui.ide.prefs 文件里面有下面这段,我们可以从 RECENT_WORKSPACES 里面看到它列出 ...

  10. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...