LeetCode题解:Container With Most Water
2015-08-28 10:53
441 查看
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.
题意:给定n个正整数作为纵坐标,每个数在横坐标上相隔1,求构成的最大矩形
解题思路:最大矩形必然左右矩形纵坐标大于等于中心矩形的纵坐标
代码:
Note: You may not slant the container.
题意:给定n个正整数作为纵坐标,每个数在横坐标上相隔1,求构成的最大矩形
解题思路:最大矩形必然左右矩形纵坐标大于等于中心矩形的纵坐标
代码:
public class Solution { public int maxArea(int[] height) { if(height == null || height.length == 0){ return 0; } int max = 0; int left = 0; int right = height.length - 1; while(left < right){ int temp = Math.min(height[left], height[right]) * (right - left); if(temp > max){ max = temp; } if(height[left] < height[right]){ ++left; }else{ --right; } } return max; } }
相关文章推荐
- 对select()参数fdset的完整理解 http://blog.csdn.net/maray/article/details/8285775
- LeetCode题解:Climbing Stairs
- OGG-01705 input trail file './dirdat/fc075295' is greater than the size of the file
- Windows Server 2008 如何部署Domain Controller
- MediaInfo参数大全
- http://blog.csdn.net/woshiyjk/article/details/7895888
- linux中waitpid系统调用
- Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostn
- UVA10976 Fractions Again?!
- [转]do_select()函数分析,理解select(),poll(),poll_wait()函数的关系
- hdu4393 Throw nails(只用模拟前面500来次,后面根据速度、位置、id值排序即可)
- UVA 12238 Anti-Rhyme Pairs(hash+二分)
- OC:copy 与 retain 的区别
- hdu 4630 No Pain No Game (区间gcd相关x线段树or树状数组)
- odoo(Openerp)如何在字段的on_change方法中动态返回domain
- 浅析cookie-free domain这么回事
- [leetcode] 11.Container With Most Water
- 什么会在main函数之前执行
- hdu 5349 MZL's simple problem 2015 Multi-University Training Contest 5
- 1302 The Snail【模拟】