Skip to content

Commit 36f2463

Browse files
committed
Add C++ solution for leetcode 11.
1 parent 6ff2df9 commit 36f2463

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int maxArea(vector<int>& height) {
9+
int len = height.size();
10+
int left = 0;
11+
int right = len - 1;
12+
13+
int maxArea = INT_MIN;
14+
while(left < right)
15+
{
16+
int area = min(height[left], height[right]) * (right - left);
17+
if(area > maxArea)
18+
maxArea = area;
19+
20+
if(height[left] <= height[right])
21+
left++;
22+
else right--;
23+
}
24+
return maxArea;
25+
}
26+
};
27+
28+
// Test
29+
int main()
30+
{
31+
Solution sol;
32+
vector<int> nums = {1,8,6,2,5,4,8,3,7};
33+
int res = sol.maxArea(nums);
34+
cout << res << endl;
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)