题解 | #盛水最多的容器#
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47?tpId=295&tqId=2284579&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
C语言
int maxArea(int* height, int heightLen ) {
// write code here
int maxRet = 0, temp;
for(int i=0; i<heightLen; i++){
for(int j=heightLen-1; j>i; j--){
//剪枝,左边固定的高度,乘左右的宽度小于最大值maxRet,此时直接跳过该轮左边的循环
//因为即使右边的高度大于左边,也是以最低边为上限(即左边)。后面j不断缩小,更不可能得出更大的容器
if(height[i] * (j-i) < maxRet)
break;
temp = (height[j] <= height[i] ? height[j] : height[i])*(j-i);
maxRet = maxRet > temp ? maxRet : temp;
}
}
return maxRet;
}
