改为手动输入矩阵int main() { vector<int> heights = {1, 1, 1, 1, 1, 1, 1, 1}; cout << largestRectangleArea(heights) << endl; return 0;
时间: 2023-11-22 19:55:55 浏览: 65
好的,那么手动输入矩阵的代码如下:
```
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> left(n), right(n);
stack<int> s;
for (int i = 0; i < n; i++) {
while (!s.empty() && heights[s.top()] >= heights[i]) {
s.pop();
}
left[i] = s.empty() ? -1 : s.top();
s.push(i);
}
while (!s.empty()) {
s.pop();
}
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && heights[s.top()] >= heights[i]) {
s.pop();
}
right[i] = s.empty() ? n : s.top();
s.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, (right[i] - left[i] - 1) * heights[i]);
}
return ans;
}
int main() {
vector<int> heights = {1, 1, 1, 1, 1, 1, 1, 1};
cout << largestRectangleArea(heights) << endl;
return 0;
}
```
输出结果为 8,因为所有柱子的高度都是 1,所以最大矩形面积为 $1 \times 8 = 8$。
阅读全文