-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContainer-With-Most-Water.cpp
56 lines (53 loc) · 1.25 KB
/
Container-With-Most-Water.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// Source : https://leetcode-cn.com/problems/container-with-most-water/
/// Author : bryce
/// Time : 2019-11-2
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
//C++ Solution 1:
class Solution {
public:
int maxArea(vector<int>& height) {
int n = height.size();
int res = 0;
int i, j = 0;
if (n < 2) return 0;
for (i = 0; i < n; i++)
{
for(j = i; j < n; j++)
{
int width = j - i;
int high = min(height[i],height[j]);
res = max(high * width, res);
}
}
return res;
}
};
// C++ Solution 2:
class Solution {
public:
int maxArea(vector<int>& height) {
int n = height.size();
int res = 0;
int i, j = 0;
if (n < 2) return 0;
for (i = 0, j = n-1; i < j;)
{
int width = j - i;
int high = min(height[i],height[j]);
res = max(high * width, res);
if (height[i] > height[j])
j--;
else
i++;
}
return res;
}
};
int main() {
vector<int> height = {1,8,6,2,5,4,8,3,7};
cout << Solution().maxArea(height) << endl; // false
return 0;
}