LeetCode 11. Container With Most Water (装最多水的容器)
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.
题目标签:Array
这道题目给了我们一个height array, 每一个数字代表一条竖直线的高度,从x轴开始。一开始用暴力法没通过。所以要用特别方法来做。分析一下,如何才能够拿到更多的水。如果一根线很低,另外一根很高,那么水只能装到低的那根线,等于把高出的区域浪费了。那么需要找到更高的线来替代低的那条才有希望找到装水更多的两条线。设两个pointer,left 和 right。 从最两边开始遍历,每一次都计算面积,比max大的话就替换。接着把低的那根线的pointer 向另外一边移动,直到两个pointers 相遇。
Java Solution:
Runtime beats 31.31%
完成日期:07/11/2017
关键词:Array
关键点:Two Pointers
public class Solution
{
public int maxArea(int[] height)
{
int left = 0;
int right = height.length-1;
int max_area = -1; while(left < right)
{
int len = right - left;
int ht = Math.min(height[left], height[right]);
max_area = Math.max(max_area, len*ht); if(height[left] < height[right])
left++;
else
right--; } return max_area;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 11. Container With Most Water (装最多水的容器)的更多相关文章
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LeetCode]11. Container With Most Water 盛最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 11. Container With Most Water(装最多的水 双指针)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 011 Container With Most Water 盛最多水的容器
给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们 ...
- Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- lintcode:装最多水的容器
装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
随机推荐
- ASP.NET Core 运行原理剖析
1. ASP.NET Core 运行原理剖析 1.1. 概述 1.2. 文件配置 1.2.1. Starup文件配置 Configure ConfigureServices 1.2.2. appset ...
- testTenuringThreshold()方法的分析与问题处理
代码如下: public class TestTenuringThreshold { private static final int _1MB = 1024 * 1024; /** * vm-arg ...
- idea导出war包
使用idea一个月了还没有用到导出war,今天突然需要我来部署测试war包,想使用myeclipse的,转念一想太掉价了 废话少说,直接上菜 如果你没有第一步操作我建议你配置一下你的idea 当然还有 ...
- ios开发——实用技术篇&三维旋转动画
实现三位旋转动画的方法有很多种,这里介绍三种 一:UIView 1 [UIView animateWithDuration:1.0 animations:^{ 2 self.iconView.laye ...
- pycharm 2017新建文件添加编码方式等
file->setting->Editor->File and Code Templates->Python Script 添加 #!/usr/bin/python3# -*- ...
- BigDecimal的加减乘除及比较大小
import java.math.BigDecimal; import static java.lang.System.out; public class BaseClass { public sta ...
- 开博近一年的感想 by 程序员小白
/* 好吧,这里的写博客应该理解为更宏观的写文章. */ 在去年的这个时候,我所知道的平台只有 CSDN 和博客园..然而 CSDN 的广告实在是不想吐槽了,选择博客园是一件非常自然的事情.要说开 ...
- SYSTEM_INFO
SYSTEM_INFO结构体包含了当前计算机的信息.这个信息包括计算机的体系结构.中央处理器的类型.系统中中央处理器的数量.页面的大小以及其他信息. SYSTEM_INFO,Win32 API函数Ge ...
- java一些问题的思考
1.思考 为什么java规定作为程序入口点的main() 方法静态的? 在java中,main()方法是java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这 ...
- cnblog排版
记录一些自己写博客中的排版技巧 一.标题 红色部分代码用在标题的CSS样式中 <p style="background: gray; font-size: 18px; font-fam ...