-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_maxArea.cpp
More file actions
33 lines (28 loc) · 768 Bytes
/
11_maxArea.cpp
File metadata and controls
33 lines (28 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
int maxArea(std::vector<int>& height) {
int ptrL = 0;
int ptrR = height.size() - 1;
int maxArea = 0;
int curArea = 0;
int width = ptrR - ptrL;
int minValue = 0;
while (ptrL < ptrR) {
minValue = std::min(height[ptrL], height[ptrR]);
width = ptrR - ptrL;
if (height[ptrL] <= height[ptrR]) {
//ptrL shorter than ptrR
curArea = minValue * width;
ptrL++;
}
else {
curArea = minValue * width;
ptrR--;
}
if (curArea >= maxArea) {
maxArea = curArea;
}
}
return maxArea;
}
};