Container With Most Water容器盛水
2016-08-18 10:18
330 查看
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.
大概意思就是给你个数组,下标代表x坐标,数组元素代表相应的y坐标值。
从y坐标向x轴引垂线,求两条垂线,使得他们围成的容器装水最多。
双层for循环那种暴力解法肯定不会满足时间要求。所以尽量选择时间复杂度为O(n)的解法。
解题思路:首尾两个指针分别往中间移动,由于求装水最多,所以求area时,
只考虑对比的两条垂线中最短的那条就可以,每次都记录相应的area。
最后返回最大的area即可。首先想到了下面的第一个方法,但是这个方法还是超过了时间限制。如下:
public class Solution { public int maxArea(int[] height) { if (height.length<2) { return 0; } int start = 0; int end = height.length-1; int area = 0; while (start < end) { int v = Math.min(height[start], height[end])*(end-start); area =Math.max(area,v); if (height[start]<height[end]) { start++; }else { end--; } } return area; } }
然后,仔细考虑,发现,其实在移动的时候,以start为例,
当移动start时,它右边的值如果比start所在位置的元素小的时候,可以不用计算了,
因为它不会在比start位置的元素求的area大。
end方向同理。继续优化上面的方法得到下面的代码:
public class Solution {
public int maxArea(int[] height) {
if (height.length<2) {
return 0;
}
int start = 0;
int end = height.length-1;
int area = 0;
while (start < end) {
int v = Math.min(height[start], height[end])*(end-start);
area =Math.max(area,v);
if (height[start]<height[end]) {
int k = start;
while (k < end && height[k]<=height[start]) {//只要start右边的元素比start所在位置的元素小
k++; //就可以不用在算面积,因为不会比start所在位置的元素求出的面积大
}
start = k;
}else {
int k = end;
while (k > start && height[k]<=height[end]) {//只要end左边的元素比end所在位置的元素小
k--; //就可以不用在算面积,因为不会比end所在位置的元素求出的面积大
}
end=k;
}
}
return area;
}
}
相关文章推荐
- boundingRectWithSize:options:attributes:context:代替sizeWithFont:constrainedToSize:lineBreakMode:
- AIC23使用过程中总结的知识点
- [AIR]批量修改目录中图片的操作方式
- Imaging Library not available, unable to import bitmaps only jpegs
- QPainter
- UVA10976 Fractions Again?!【水题】
- POJ 3411 Paid Roads
- LeetCode--No.219--Contains Duplicate II--有个地方不太懂
- HDU 4756 Install Air Conditioning(树形dp+MST)
- Ext.container.Container.add(): Cannot add destroyed item 'XXX' to Container 'YYY'
- device team0 -failure to get interface.....
- XMPP 常见错误:<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
- BIO\NIO\AIO记录
- 巨头纷纷进击人工智能,看华为小米们如何探索未来
- 【codeforces】29C—Mail Stamps
- Thumbnails操作图片发红的问题解决
- UVA-442 Matrix Chain Multiplication
- 【USACO题库】5.2.1 Snail Trails蜗牛的旅行
- 木雨音乐 项目开发(三)主界面MainActivity
- Lock锁,一次唤醒所有线程低性能的问题