-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-Container-With-Most-Water.cpp
70 lines (68 loc) · 2.39 KB
/
11-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
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* 方法二时间更快,i从前开始遍历,j从后开始遍历,如果当前height[i]比之前的i的最高值小或相等,
* 那么无论如何当前i和之后的j组成的container的大小都不会是max。
* 又因为j从后往前遍历,只要遇到当前height[j]比当前height[i]大,
* 那么无论如何当前i和之后的j组成的container的大小都不会是max,
* 所以遇到这种情况只要比较当前i和j组成的container与max然后跳出循环即可。
* 而且对于当前i,如果当前height[j]比之前遍历的j小或相等,
* 那么无论如何当前i和j组成的container的大小都不会是max
*/
class Solution {
public:
int maxArea(vector<int>& height) {
int length = height.size(), i, j, max = 0, temp, maxHeight;
for(i = 1; i < length; i++){
temp = height[i];
maxHeight = 0;
for(j = 0; j < i; j++){
if(height[j] > maxHeight){
if(height[j] < temp){
maxHeight = height[j];
if(height[j] * (i - j) > max){
max = height[j] * (i - j);
}
}
else{
if(temp * (i - j) >= max){
max = temp * (i - j);
}
break;
}
}
}
}
return max;
}
};
class Solution {
public:
int maxArea(vector<int>& height) {
int length = height.size(), i, j, max = 0, temp = 0, maxHeight;
for(i = 0; i < length - 1; i++){
if(temp < height[i]){
temp = height[i];
}
else{
continue;
}
maxHeight = 0;
for(j = length - 1; j > i; j--){
if(height[j] > maxHeight){
if(height[j] < temp){
maxHeight = height[j];
if(height[j] * (j - i) > max){
max = height[j] * (j - i);
}
}
else{
if(temp * (j - i) >= max){
max = temp * (j - i);
}
break;
}
}
}
}
return max;
}
};