Container With Most Water 解答
Question
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.
Solution
Since the area size is largely depended on shorter height, so we use two pointers. Time complexity O(n), space cost O(1).
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length < 2)
return 0;
int left = 0, right = height.length - 1;
int result = 0, tmp = 0;
while (left < right) {
tmp = (right - left) * Math.min(height[left], height[right]);
result = Math.max(result, tmp);
if (height[left] <= height[right])
left++;
else
right--;
}
return result;
}
}
Container With Most Water 解答的更多相关文章
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 67. Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 关于Container With Most Water的求解
Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
- Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...
随机推荐
- CString 的一些事
MFC Visual Studio 2008 CString 的 Format 中不能这样存在str.Format(_T("Cool(\%)")); 或者 str.Format( ...
- SQL Server (MSSQLSERVER) 服务因 找不到指定的模块。 服务特定错误而停止。
新装了sql server 2008,发现sqlserver 服务没法起来.查看系统日志是7024如下: 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 ...
- Handler消息机制实现更新主UI
如下实现的是简单的更新主UI的方法,用Handler消息机制 将textview的内容每隔一秒实现num++ /* * handler消息机制 * asynctask异步任务 * * httpcli ...
- handsontable常规配置的中文API
常规配置: 1.固定行列位置 fixedRowsTop:行数 //固定顶部多少行不能垂直滚动 fixedColumnsLeft:列数 //固定左侧多少列不能水平滚动 2.拖拽行头或列头改变行或列的大小 ...
- [think in java]知识点学习
java中 全部数值都有正负号,不存在无符号整数. java中的基本类型存储在堆栈中. 其它对象存储在堆中. java确保数组会被初始化,并且不能在它的范围之外被訪问. 下面代码在c和c++中是合法的 ...
- 连数据库是ODBC好还是OLEDB好
1.连数据库是ODBC好还是OLEDB好?2.是不是只有微软的数据库才可以用OLEDB?3.要切换这两种连接,是不是只需要修改连接字符串?谢谢大家了,小弟对这三个问题不解 分享到: 2009-03 ...
- mvc分页生成静态页,mvc生成静态页
http://blog.csdn.net/xxj_jing/article/details/7899125 分页生成静态页 http://www.cnblogs.com/luanyilin/archi ...
- MVC 数据列表显示插件大全
Jgrid 官网示例: http://www.trirand.net/demo/aspnet/mvc/jqgrid/ Code Project示例: http://www.codeproject.co ...
- javascirpt IP验证
js IP 端口验证 function isPort(str) { var parten=/^(\d)+$/g; if(parten.test(str)&&parseI ...
- Python学习笔记6(列表生成式)
1.生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3 ...