LeetCode 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.

 /*************************************************************************
> File Name: LeetCode011.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: Wed 27 Apr 2016 02:11:36 AM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> int maxArea(int* height, int heightSize) { int left = ;
int right = heightSize - ;
int max = ; while( left < right ){ int water = ( right - left ) * ( height[left] < height[right] ? height[left++] : height[right--] );
max = max > water ? max : water;
printf("Each water is %d\n", water);
}
printf("Max water is %d\n", max);
return max; } int main()
{
int heights[] = {, , , , , , , };
int size = ; maxArea( heights, size ); return ;
}
上一篇:html5之history对象 控制浏览器前进或后退事件


下一篇:图片首尾平滑轮播(JS原生方法—节流)<原创>