This commit is contained in:
youngyangyang04
2020-10-20 15:40:54 +08:00
parent 3e39d7f897
commit 41656d1827
8 changed files with 367 additions and 24 deletions

View File

@@ -0,0 +1,31 @@
# 链接
https://leetcode-cn.com/problems/largest-rectangle-in-histogram/
## 思路
```
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int sum = 0;
for (int i = 0; i < heights.size(); i++) {
int left = i;
int right = i;
for (; left >= 0; left--) {
if (heights[left] < heights[i]) break;
}
for (; right < heights.size(); right++) {
if (heights[right] < heights[i]) break;
}
int w = right - left - 1;
int h = heights[i];
sum = max(sum, w * h);
}
return sum;
}
};
```
如上代码并不能通过leetcode超时了因为时间复杂度是O(n^2)。