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.
其实就是求两根杆子之间能容纳的水量,不用考虑中间的杆子。
26ms
class Solution { public: int maxArea(vector<int>& height) { if (height.empty()){ return 0; } int l = 0, h = height.size() - 1; int max = (h - l)*(height[l] > height[h] ? height[h] : height[l]); while (l < h){ if (height[l] > height[h]){ h--; } else{ l++; } int temp = (h - l)*(height[l] > height[h] ? height[h] : height[l]); if (temp > max){ max = temp; } } return max; } };