Container With Most Water(LintCode)
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.
Given [1,3,2]
, the max area of the container is 2
.
You may not slant the container.
从数组两头遍历,计算面积,然后从小的那一段a开始找到下一个比a大的元素。直到所有数组都被遍历。
public class Solution {
/**
* @param heights: an array of integers
* @return: an integer
*/
public int maxArea(int[] heights) {
int l = heights.length;
if (l == 0) {
return 0;
}
int max = -1;
int i = 0;
int j = l-1;
while (i < j) {
int v = (j - i) * Math.min(heights[i],heights[j]);
if (max < v) {
max = v;
} if(heights[i] > heights[j]){
int x = heights[j];
while (heights[j] <= x && i < j) j--;
}else {
int x = heights[i];
while(heights[i] <= x && i < j) i++;
}
}
return max;
}
}
Container With Most Water(LintCode)的更多相关文章
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- #LeetCode# Container With Most Water (todo)
描述: 实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题) 代码: def maxArea(self, height): tmp = len ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- 在Azure Container Service创建Kubernetes(k8s)群集运行ASP.NET Core跨平台应用程序
引子 在此前的一篇文章中,我介绍了如何在本地docker环境中运行ASP.NET Core跨平台应用程序(http://www.cnblogs.com/chenxizhang/p/7148657.ht ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 22.Container With Most Water(能装最多水的容器)
Level: Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...
- LeetCode 11 Container With Most Water(分支判断问题)
题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description Problem: 已知n条垂直于x轴的线 ...
- 11. Container With Most Water(头尾双指针)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Implement Trie(LintCode)
Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assume ...
随机推荐
- Java反射中method.isBridge() 桥接方法
桥接方法是 JDK 1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法.我们可以通过Method.isBridge()方法来判断一个方法是 ...
- 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial
[题目]洛谷10月月赛R1 提高组 [题意]求n!在k进制下末尾0的个数,n<=1e18,k<=1e16. [题解]考虑10进制末尾0要考虑2和5,推广到k进制则将k分解质因数. 每个质因 ...
- 基本控件文档-UIKit结构图---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 UIKit结构图 //转载请注明出处--本文永久链接:http://www.cnbl ...
- Computer(HDU2196+树形dp+树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...
- 变量对象vs活动对象
这是我见过描述的最为详尽的关于变量对象.活动对象以及闭包的解析,来自知乎,感谢答主: 作者:闭家锁链接:https://www.zhihu.com/question/36393048/answer/7 ...
- python 正则表达式口诀
正则其实也势利,削尖头来把钱揣: (指开始符号^和结尾符号$) 特殊符号认不了,弄个倒杠来引路: (指\. \*等特殊符号) 倒杠后面跟小w, 数字字母来表示: (\w跟数字字母;\d跟数字) ...
- 分布式队列Celery
Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...
- Django rest framework 版本控制(源码分析)
基于上述分析 #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进行限制 self.initial(request, *args, **kwargs) #2.1处理版本信息 #versi ...
- python基础===* 解包,格式化输出和print的一点知识
python3中的特性: >>> name = "botoo" >>> print(f"my name is {name}" ...
- 64_l1
L-function-1.23-18.fc26.i686.rpm 13-Feb-2017 23:19 154562 L-function-1.23-18.fc26.x86_64.rpm 13-Feb- ...