011 Container With Most Water
2016-05-06 23:46
453 查看
Given n non-negative integers a1, a2,..., an, where each represents a point at coordinate (i, ai). n verticallines 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个高度不同的数作为为Y坐标,以序列1到n为X坐标,这样的点(i,ai)分别和(i,0)组成的线段有n条,从中选取两条,以最短的一条为高,线段在X轴上的距离为宽,求最大的矩形面积。时间复杂度为O(n)解题思路:两条线段,哪个短,就换哪个
int maxArea(int* height, int heightSize) {int i,j,max,area;i=0;j=heightSize-1;max=0;while(i<j){area=(height[i]>height[j]?height[j]:height[i])*(j-i);if(area>max){max=area;}if(height[i]>height[j]){j--;}else{i++;}}return max;}
相关文章推荐
- 使用 MailOtto 做完美预加载
- Maven Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean Failed to delete access_log
- jetbrains系列IDE-Vmoptions 优化指南
- INSTALL_FAILED_CPU_ABI_INCOMPATIBLE -- 使用Genymotion出现错误
- container_of()宏
- E: Some index files failed to download. They have been ignored, or old ones used instead.
- 介绍几款AI开源平台
- 分类器性能评估之Lift和Gain(3)
- HDU 1021 Fibonacci Again(找规律)
- IAR的STlink下载出现 Failed to set configuration with MCU name STM8S207MB: SWIM error [30006]:解决办法
- RDD中的转换和action(二)PairRDD操作
- wait to explain
- 有n根棍子,棍子的长度为ai。想从中选出三个组成周长尽可能唱的三角形。
- 编译报错 does not contain bit code
- Killing container
- 出现( linker command failed with exit code 1)错误 静态变量重名的解决方法
- Exception in thread "main" org.hibernate.AnnotationException: List/array has to be annotated with an
- RMAN备份动态性能视图 V$BACKUP_SET_DETAILS
- 出现( linker command failed with exit code 1)错误总结
- http://blog.csdn.net/yaoxtao/article/details/42671971