盛最多的水的容器

/*
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。
在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
*/
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include <iostream>
int getMax(int a,int b){
    if(a>b)
        return a;
    return b;
}
int getMin(int a,int b){
    if(a<b)
        return a;
    return b;
}
int maxArea(int* height, int heightSize){
    int maxarea=0,left=0,right=heightSize-1;
    while(left<right){
        maxarea=getMax(maxarea,getMin(height[left],height[right])*(right-left));
        if(height[left]<height[right]){
            left++;
        }else{
            right--;
        }
    }
    return maxarea;
}
int main()
{
    int h[]={1,8,6,2,5,4,8,3,7};
    int n =maxArea(h,9);
    printf("%d\n",n);
    return 0;
}

 

posted @ 2020-03-29 15:29  dreamy_java  阅读(134)  评论(0编辑  收藏  举报