短板
描述:🤔
错误代码
c语言代码⬇️
噫,好,我有了
int min(int a, int b){
if(a < b)
return a;
return b;
}
int maxArea(int* height, int heightSize){
int max = -1;
for(int i = 0; i < heightSize; i++){
for(int j = i+1; j < heightSize; j++){
int h = min(height[i], height[j]);
if((j - i)*h > max){
max = (j - i)*h;
}
}
}
return max;
}
错误原因:暴力美学大失败,超时不给过
通过代码
通过代码1⬇️
对撞指针,无聊
int lowerIndex(int *nums, int i, int j){
if(nums[i] < nums[j])
return i;
return j;
}
int maxArea(int* height, int heightSize){
int maxValume = -1;
int i = 0;
int j = heightSize - 1;
while(i < j){
int lowerindex = lowerIndex(height, i, j);
int valume = (j - i)*height[lowerindex];
if(valume > maxValume){
maxValume = valume;
}
if(j == lowerindex){
j--;
}else{
i++;
}
}
return maxValume;
}
通过代码2⬇️
总结
总结: