Container With Most Water LeetCode solution in C++

1
class Solution {
2
public:
3
int maxArea(vector<int> &height) {
4
int frontHeight, rareHeight, minHeight, area, distance;
5

6
int front = 0;
7
int rare = height.size() - 1;
8
int maxArea = 0;
9

10
while (front < rare) {
11
frontHeight = height[front];
12
rareHeight = height[rare];
13

14
if (frontHeight < rareHeight)
15
minHeight = frontHeight;
16
else
17
minHeight = rareHeight;
18

19
distance = rare - front;
20
area = minHeight * distance;
21

22
if (area > maxArea) maxArea = area;
23

24
if (frontHeight > rareHeight)
25
rare--;
26
else
27
front++;
28
}
29

30
return maxArea;
31
}
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0