From 8dc2f5e9a5bcd239005cbac212ae4d019ce550f1 Mon Sep 17 00:00:00 2001 From: carry Date: Thu, 13 Feb 2025 22:00:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=9B=E6=9C=80=E5=A4=9A=E6=B0=B4=E7=9A=84?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=EF=BC=88=E5=8F=8C=E6=8C=87=E9=92=88=E6=B3=95?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maxArea.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/maxArea.cpp b/maxArea.cpp index 7953fd9..aab015f 100644 --- a/maxArea.cpp +++ b/maxArea.cpp @@ -9,18 +9,21 @@ public: int maxArea(vector &height) { int maxArea = 0; - for(int i = 0; i < height.size(); i++) - { - for(int j = i + 1; j < height.size(); j++) - { - int area = min(height[i], height[j]) * (j - i); - maxArea = max(maxArea, area); + int left = 0; + int right = height.size() - 1; + while (left < right){ + maxArea = max(maxArea,(right - left)*min(height[right] , height[left])); + if( height[left] < height[right]){ + left++; + } + else{ + right--; } } + return maxArea; } }; - int main() { vector height = {1, 8, 6, 2, 5, 4, 8, 3, 7};