-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_LC11.cpp
More file actions
44 lines (38 loc) · 878 Bytes
/
Copy pathvector_LC11.cpp
File metadata and controls
44 lines (38 loc) · 878 Bytes
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
/*
LC11
container with most water
*/
#include <iostream>
using namespace std;
#include <vector>
class Solution {
private:
int min(int a, int b) {
return (a < b) ? a : b;
}
public:
int maxArea(vector<int>& height) {
//the most left and right height
int heightMin = height[0], heightMax = height[height.size() - 1];
int area = min(heightMin, heightMax)*(height.size() - 1);
int temp = 0;
int left = heightMin;
for (int i = 0; i < height.size()-1; i++) {
if (height[i] <left) continue;
int right = heightMax;
for (int j = height.size() - 1; j >= i + 1; j--) {
if (height[j] < right) {
continue;
}
temp = min(height[i], height[j]) * (j - i);
if (temp >= area) {
area = temp;
right = height[j];
left = height[i];
}
else continue;
}
}
return area;
}
};