mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-22 21:43:08 +03:00
31 lines
673 B
C
31 lines
673 B
C
//Fucntion to calculate min of values a and b
|
|
int min(int a, int b){
|
|
return ((a<b)?a:b);
|
|
}
|
|
|
|
//Two pointer approach to find maximum container area
|
|
int maxArea(int* height, int heightSize){
|
|
|
|
//Start with maximum container width
|
|
int start = 0;
|
|
int end = heightSize-1;
|
|
int res = 0;
|
|
|
|
while(start<end){
|
|
//Calculate current area by taking minimum of two heights
|
|
int currArea = (end-start)*min(height[start],height[end]);
|
|
|
|
if(currArea>res)
|
|
res = currArea;
|
|
|
|
if(height[start]<height[end])
|
|
start = start + 1;
|
|
else
|
|
end = end - 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|