container-with-most-water
1.0.0
Container With Most Water
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <iostream>
2
#include <vector>
3
4
using namespace
std;
5
6
class
Solution
7
{
8
public
:
9
int
maxArea
(vector<int>& height)
10
{
11
int
left = 0;
12
int
right = (int)height.size() - 1;
13
int
volume = min(height[left], height[right]) * abs(right - left);
14
while
(left < right)
15
{
16
// move pointer that points to lower value
17
if
(height[left] < height[right])
18
left++;
19
else
20
right--;
21
22
// cout << "volume: " << volume << "; new_volume: " << new_volume << endl;
23
volume = max(volume, min(height[left], height[right]) * abs(right - left));
24
}
25
return
volume;
26
}
27
};
28
29
int
main
()
30
{
31
vector<int> height = {1,8,6,2,5,4,8,3,7};
32
// vector<int> height = {1,2,1};
33
Solution
sol;
34
cout << sol.
maxArea
(height) << endl;
35
return
0;
36
}
Solution
Definition
main.cpp:7
Solution::maxArea
int maxArea(vector< int > &height)
Definition
main.cpp:9
main
int main()
Definition
main.cpp:29
main.cpp
Generated by
1.9.8