Container with most water
2015-06-17 20:28
537 查看
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.
很简单的一道题,只要理解 container =(min(a[i],a[j])*(j - i );如果从二头进行轮询,那么要比以前的值大,就必须 a[x] >min(a[i],a[j])了.
代码:
Note: You may not slant the container.
很简单的一道题,只要理解 container =(min(a[i],a[j])*(j - i );如果从二头进行轮询,那么要比以前的值大,就必须 a[x] >min(a[i],a[j])了.
代码:
int maxArea(int* height, int heightSize) { int i = 0; int j = heightSize - 1; int max=0 ; int tem =height[i] ; int container_tem = 0; int container_high = 0; while(i < j) { if( height[i]>= height[j]) {container_high = height[j]; tem =height[j]; container_tem = container_high*(j -i); max = max > container_tem?max : container_tem; while(height[--j] <tem && i < j) ; continue; }else if( height[i] < height[j]) { container_high = height[i]; tem =height[i]; container_tem = container_high*(j -i); max = max > container_tem?max : container_tem; while(height[++i] <tem && i < j) ; continue; } } return(max); }
相关文章推荐
- tail跟踪日志时打印时间
- hdu oj 4300 Clairewd’s message AC code
- [LeetCode] Contains Duplicate III
- Climbing Stairs
- 对retain 和 assign的理解
- Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授权字符串" UserInfo=0x17426e940 {NS
- hdu 1702 ACboy needs your help again!
- 从Clarifai的估值聊聊深度学习
- UVa 10551 - Basic Remains
- [AIR] 检测移动设备运动
- youwuku和koudaitong以及weimeng差异
- iphone使用keychain来存取用户名和密码
- RHEL6.4 安装 highpoint RocketRAID 2720 阵列卡驱动
- Xcode运行出错Installation Failed Invalid argument
- linux下,使用opencv训练级联分类器opencv_traincascade
- Installation error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
- 浅谈使用NIO,AIO的感受
- 关于:HTTP Header -> Content-Type: text/plain Cache-Control: no-cache IE浏览器弹出错误下载对话
- linux环境下使用opencv训练级联分类器traincascade
- hdoj 1022 Train Problem I