LeetCode 11

时间:2021-05-16 14:44:50

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 ;
}