Repository navigation
Expand file tree
/
Copy pathContainerWithMostWater.cpp
More file actions
216 lines (201 loc) · 5.47 KB
/
Copy pathContainerWithMostWater.cpp
File metadata and controls
216 lines (201 loc) · 5.47 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
/**************** This is a wrong solution *************
because it does not consider either the case of equal
value with different position or the case of small value
but large position distance
int maxArea(vector<int> &height) {
if (height.size() <= 1) {
return 0;
}
int maxH, secH;
maxH = secH = height[0];
int maxP = 0, secP = 0;
for (int i = 1; i < height.size(); ++i) {
if (height[i] > maxH) {
secH = maxH;
maxH = height[i];
secP = maxP;
maxP = i;
} else {
if (height[i] > secH) {
secH = height[i];
secP = i;
}
}
}
return secH*abs(maxP-secP);
}*/
/******************* TLE version *******************
int maxArea(vector <int> &height) {
if (height.size() <= 1) {
return 0;
}
int maxArea = 0;
for (int i = 0; i < height.size(); ++i) {
for (int j = i+1; j < height.size(); ++j) {
int tmpArea = (j-i)*min(height[i], height[j]);
if (maxArea < tmpArea) {
maxArea = tmpArea;
}
}
}
return maxArea;
}*/
/***************** Passed, but rather ugly many restrictions attached method *******************
int maxArea(vector <int> &height) {
if (height.size() <= 1) {
return 0;
}
int maxArea = 0;
int maxHeight = height[0];
vector <int> leftWallPos;
leftWallPos.push_back(0);
for (int i = 1; i < height.size(); ++i) {
maxHeight = maxHeight < height[i] ? height[i]:maxHeight;
if (height[i] > height[i-1]) {
leftWallPos.push_back(i); // This condition could be further restricted to (height[i] > leftWallPos[leftWallPos.size()-1])
}
}
vector <int> rightWallPos;
rightWallPos.push_back(height.size()-1);
for (int i = height.size()-2; i >= 0; --i) {
if (height[i] > height[i+1]) {
rightWallPos.push_back(i); // This condition could be further restricted to (height[i] > leftWallPos[leftWallPos.size()-1])
}
}
// The following code could be further optimized if we keep record of
// Maxheight not only for the whole sequence but rather all those after
// leftWallPos[i] and all those before rightWallPos[j];
for (int i = 0; i < leftWallPos.size(); ++i) {
if (maxArea >= maxHeight*(height.size()-leftWallPos[i]-1)) {
break;
}
for (int j = 0; j < rightWallPos.size(); ++j) {
if (rightWallPos[j] <= leftWallPos[i] || maxArea >= maxHeight*rightWallPos[j]) {
break;
}
int tmpArea = (rightWallPos[j]-leftWallPos[i]) * min(height[rightWallPos[j]],height[leftWallPos[i]]);
if (maxArea < tmpArea) {
maxArea = tmpArea;
}
}
}
/*************** Another TLE version **********************
for (int i = height.size()-1; i > 0; --i) {
if (maxArea >= maxHeight*i) {
break;
}
for (int j = 0; j+i <height.size(); ++j) {
int tmpArea = i*min(height[j+i], height[j]);
if (maxArea < tmpArea) {
maxArea = tmpArea;
}
}
}
return maxArea;
}*/
/************* A further optimized method of the previous one, but still not the best ************
int maxArea(vector <int> &height) {
if (height.size() <= 1) {
return 0;
}
int maxArea = 0;
vector <int> leftWallPos;
int maxLeft[height.size()];
leftWallPos.push_back(0);
maxLeft[0] = height[0];
for (int i = 1; i < height.size(); ++i) {
maxLeft[i] = max(maxLeft[i-1], height[i]);
if (height[i] > height[leftWallPos[leftWallPos.size()-1]]) {
leftWallPos.push_back(i);
}
}
vector <int> rightWallPos;
int maxRight[height.size()];
rightWallPos.push_back(height.size()-1);
maxRight[height.size()-1] = height[height.size()-1];
for (int i = height.size()-2; i >= 0; --i) {
maxRight[i] = max(maxRight[i+1], height[i]);
if (height[i] > height[rightWallPos[rightWallPos.size()-1]]) {
rightWallPos.push_back(i);
}
}
for (int i = 0; i < leftWallPos.size(); ++i) {
if (maxArea >= maxRight[leftWallPos[i]]*(height.size()-leftWallPos[i]-1)) {
break;
}
for (int j = 0; j < rightWallPos.size(); ++j) {
if (rightWallPos[j] <= leftWallPos[i] || maxArea >= maxLeft[rightWallPos[j]]*rightWallPos[j]) {
break;
}
int tmpArea = (rightWallPos[j]-leftWallPos[i]) * min(height[rightWallPos[j]],height[leftWallPos[i]]);
if (maxArea < tmpArea) {
maxArea = tmpArea;
}
}
}
return maxArea;
}*/
// A nice O(NlgN) solution, though there exists an even better O(N) solution
/*struct Node {
int height;
int indx;
Node (int h, int ii):height(h), indx(ii){}
friend bool operator < (Node a, Node b) {
return a.height > b.height;
}
};
int maxArea(vector <int> &height) {
if (height.size() <= 1) {
return 0;
}
vector <Node> nodes;
for (int i = 0; i < height.size(); ++i) {
nodes.push_back(Node(height[i], i));
}
sort(nodes.begin(), nodes.end());
int left = min(nodes[0].indx, nodes[1].indx);
int right = max(nodes[0].indx, nodes[1].indx);
int maxArea = nodes[1].height*(right-left);
for (int i = 2; i < nodes.size(); ++i) {
int indx = nodes[i].indx;
int width = 0;
if (indx > right) {
right = indx;
width = right - left;
} else if (indx < left) {
left = indx;
width = right - left;
} else {
width = max(indx-left, right-indx);
}
maxArea = max(maxArea, nodes[i].height*width);
}
return maxArea;
}*/
// The Final BEST SOLUTION, O(N) complexity
int maxArea(vector <int> &height) {
if (height.size() <= 1) {
return 0;
}
int left = 0;
int right = height.size() - 1;
int maxArea = 0;
while (left < right) {
maxArea = max(maxArea, min(height[left], height[right])*(right-left));
if (height[left] > height[right]) {
right--;
} else {
left++;
}
}
return maxArea;
}
int main() {
return 0;
}