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 ...
随机推荐
- 图论&数学:矩阵树定理
运用矩阵树定理进行生成树计数 给定一个n个点m条边的无向图,问生成树有多少种可能 直接套用矩阵树定理计算即可 矩阵树定理的描述如下: 首先读入无向图的邻接矩阵,u-v G[u][v]++ G[v][u ...
- 【bzoj2038-小z的袜子】莫队算法
莫队例题. 莫队学习:https://www.cnblogs.com/Paul-Guderian/p/6933799.html 本题 分子是sigma(c(sum[a[i]],2)),分母是sigma ...
- NGINX: 配置 HSTS
参考: [ 浅析 HSTS - 博客园 ] [ HTTP HSTS协议和 nginx - 运维生存时间] [ HSTS ] Header: Strict-Transport-Security Stri ...
- C++正则表达式例子
给了策划配置公式的地方,需要将策划配置的公式文本转化为可执行的脚本代码:比如:self->mHp*2+target->2mMp*GetHit()+ self_mon->4mDan/1 ...
- bzoj 1406 数论
首先问题的意思就是在找出n以内的所有x^2%n=1的数,那么我们可以得到(x+1)(x-1)=y*n,那么我们知道n|(x+1)(x-1),我们设n=a*b,那么我们对于任意的a,我们满足n%a==0 ...
- Webview 中FaultyInfo代码说明
class FaultyInfoHandler(tornado.web.RequestHandler): def get(self): import xmlrpc.client s = xmlrpc. ...
- CursorFileManager对cursor文件的读写
public class CursorFileManager implements CursorManager{public void write(String key, LongCursor cur ...
- Java多线程学习(一)Java多线程入门
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79640870 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- php中的base64写shell
<?php system(base64_decode($_GET['info'])); #http://localhost/1.php?info=d2hvYW1p #这只是一个例子 ?>
- thread线程栈size及局部变量最大可分配size【转】
转自:http://blog.csdn.net/sunny04/article/details/46805261 版权声明:本文为博主原创文章,未经博主允许不得转载. 进程是操作系统的最小资源管理单元 ...