1. Description
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.
2. Example
input height[] = { 1, 1 }, heightSize = 2
output 1
3. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="C S= O(1)" active="active"]
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; }
[/restab]
[restab title="C (TEST)"]
#includeint 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; }
[/restab]
[/restabs]
Comments | NOTHING