Calculate the maximum water holding area
For example: {1,8,6,2,5,4,8,3,7};
Output: 49;
Method : Use the head-to-tail pointer movement method and adhere to the principle of short wooden barrels. The volume of water depends on the short board. The smaller head and tail pointers move. During the movement of the pointer, the calculated maximum value can be retained.
int MaxArea(int *s,int len)
{
int MaxSum= 0;
int tail = len-1;
int head = 0;
while(head !=tail)//头尾指针相遇则退出循环
{
int temp = 0;
if(s[head]>s[tail])//头指针大于尾指针
{
temp = (tail-head)*(s[tail]>s[head]?s[head]:s[tail]);
tail--;
}else{
temp = (tail-head)*(s[tail]>s[head]?s[head]:s[tail]);
head++;
}
if(temp>MaxSum)
MaxSum = temp;
}
return MaxSum;
}