方法一:在看题解之前我所写的代码用的是贪心的思想,然后时间复杂度和空间复杂度很高,具体思路是这样的。
我先把所有的柱状条按照高度排序,比较高的在前面。之后再从高的柱状条开始组合、遍历。而如果全部扫一遍的话,就是纯暴力了,我的想法是,如果到某个高度h后,如果
h
∗
l
<
r
e
s
u
l
t
h*l<result
h∗l<result,也就是当前高度乘上最长的宽度也不会比现在最大的容量大了,遍历就结束了。
class Solution {
public:
struct Node{
int h;
int index;
Node(){}
Node(int aa, int ii) {
h = aa;
index = ii;
}
};
static bool cmp(Node n1, Node n2) {
if (n1.h != n2.h) return n1.h > n2.h;
return n1.index < n2.index;
}
int maxArea(vector<int>& height) {
int result = 0;
vector<Node> h_in;
int l = height.size() - 1;
for (int i = 0; i <= l; i++) {
h_in.push_back(Node(height[i], i));
}
sort(h_in.begin(), h_in.end(), cmp);
int flag = 0;
for (int i = 1; i <= l; i++) {
for (int j = 0; j < i; j++) {
if (h_in[i].h * l < result) {
flag = 1;
break;
}
int now_c = h_in[i].h * abs(h_in[i].index - h_in[j].index);
if (now_c > result) {
result = now_c;
}
}
if (flag) break;
}
return result;
}
};
方法二:指针法
题解中的方法复杂度是O(n)的,666666。
这个方法的思想是,在缩减宽度的时候,只有提升高度才有可能补偿缩减宽度带来的损失,甚至比原容量更大。所以本方法就是通过移动较低边方向的索引来在缩减宽度的同时争取获得更高的高度。在前后两个指针扫一遍之后就能得到最大的容量。
class Solution {
public:
int maxArea(vector<int>& height) {
int result = 0;
int l = height.size();
int left = 0, right = l - 1;
while (left < right) {
int area = min(height[left], height[right]) * (right - left);
if (area > result) result = area;
if (height[left] < height[right]) left++;
else right--;
}
return result;
}
};