/* * 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.
publicclassSolution{ publicintmaxArea(int[] height){ int L = 0, R = height.length - 1; int area = 0; while(L < R){ area = Math.max(area, (R - L) * Math.min(height[L], height[R])); if(height[R] > height[L]){ L++; }else{ R--; } } return area; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Solution { public: intmaxArea(vector<int>& height){ int L = 0, R = height.size() - 1; int area = 0; while (L < R) { area = max(area, min(height[L], height[R]) * (R - L)); if (height[L] > height[R]) R--; else L++; } return area; } };