https://leetcode.com/problems/largest-rectangle-in-histogram/
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int res = 0;
for (int i = 0; i < height.size(); ++i) {
if (i + 1 < height.size() && height[i] <= height[i + 1]) { //剪肢
continue;
}
int minH = height[i];
for (int j = i; j >= 0; --j) {
minH = min(minH, height[j]);
int area = minH * (i - j + 1);
res = max(res, area);
}
}
return res;
}
};
本文介绍了一种解决LeetCode上最大矩形面积问题的方法。通过遍历输入的高度数组,利用剪枝技巧优化搜索过程,寻找可以构成的最大矩形面积。此算法在遇到递增序列时跳过不必要的检查,提高了效率。

279

被折叠的 条评论
为什么被折叠?



