顿搜
飞过闲红千叶,夕岸在哪
类目归类
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
input height[] = { 1, 1 }, heightSize = 2
output 1
int maxArea(int* height, int heightSize) {
int mContain = 0;
for (int * i = height, * j = height + heightSize -1; i < j;) {
int mMinHeight = *i < *j ? *i : *j;
int mTmpContain = (j - i) * mMinHeight;
mContain = mContain < mTmpContain ? mTmpContain : mContain;
while (i < j && *i <= mMinHeight) i++;
while (i < j && *j <= mMinHeight) j--;
}
return mContain;
}#include<stdio.h>
int maxArea(int* height, int heightSize) {
int mContain = 0;
for (int * i = height, * j = height + heightSize -1; i < j;) {
int mMinHeight = *i < *j ? *i : *j;
int mTmpContain = (j - i) * mMinHeight;
mContain = mContain < mTmpContain ? mTmpContain : mContain;
while (i < j && *i <= mMinHeight) i++;
while (i < j && *j <= mMinHeight) j--;
}
return mContain;
}
int main() {
int height[] = { 1, 1 };
printf("%d\n",maxArea(height, 2));
system("pause");
return 0;
}