The minimum height controls the volumns. So let two runner at two ends start to scan the array.
1 class Solution { 2 public: 3 int maxArea(vector<int> &height) { 4 int start = 0, end = height.size()-1, result = 0; 5 while (start < end) { 6 result = max(result, min(height[start], height[end]) * (end - start)); 7 if (height[start] < height[end]) { 8 start++; 9 } else { 10 end--; 11 } 12 } 13 return result; 14 } 15 };
时间: 2024-10-10 18:07:23