-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0011_max_area.cc
43 lines (39 loc) · 964 Bytes
/
0011_max_area.cc
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
/**
* Author: Xiangyue Cai
* Date: 2019-09-04
*/
// runtime: 1280ms
// memory usage: 9.7mb
class Solution {
public:
int maxArea(vector<int>& height) {
int max_area = 0;
int size = height.size();
for(int i = 0 ; i < size; ++i){
for(int j = i + 1; j < size; ++j) {
int h = min(height[i], height[j]);
int area = h * (j - i);
max_area = area > max_area? area : max_area;
}
}
return max_area;
}
};
// runtime: 12ms
// memory usage: 9.8mb
class Solution {
public:
int maxArea(vector<int>& height) {
int max_area = 0;
int l = 0;
int r = height.size() - 1;
while (l < r) {
max_area = max(max_area, min(height[l], height[r]) * (r - l));
if (height[l] < height[r])
++l;
else
--r;
}
return max_area;
}
};