Given n non-negative integers A1, A2, ..., an, where each represents a point at Coordi Nate (i, ai). N Vertical Lines is drawn such that the the both endpoints of line I am at (i, ai) and ( i, 0). Find lines, which together with X-axis forms a container, such that the container contains the most water.
Note:you may not slant the container.
classSolution { Public: intMaxarea (vector<int> &height) { intJ=height.size ()-1, i=0, mx=0; while(i<j) {MX=max (MX, (j-i) *(min (height[i],height[j]))); if(height[i]<Height[j]) I++; Else if(height[i]>=Height[j]) J--; } returnMX; }}; Somebody else's code, eh .
classSolution { Public: intMinintAintb) {returnA>b?b:a; } intMaxarea (vector<int> &height) {Vector<int>First,last; intmax = height[0]; First.push_back (max); First.push_back (0); for(intI=0; I) { if(Height[i] >max) {Max=Height[i]; First.push_back (max); First.push_back (i); } Else Continue; } Max= Height[height.size ()-1]; Last.push_back (max); Last.push_back (Height.size ()-1); for(intI=height.size ()-1; i>=0; i--) { if(Height[i] >max) {Max=Height[i]; Last.push_back (max); Last.push_back (i); } Else Continue; } Max=0; for(intI=0; I<first.size (); i+=2) { for(intj=0; J<last.size (); j+=2) { if(min (First[i],last[j]) * (last[j+1]-first[i+1]) >max) Max= Min (First[i],last[j]) * (last[j+1]-first[i+1]); } } returnMax; }}; My Code, Hey
Problem-solving ideas: Don't say it
Leetcode Container with most water