mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
Update
This commit is contained in:
31
problems/0084.柱状图中最大的矩形.md
Normal file
31
problems/0084.柱状图中最大的矩形.md
Normal 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)。
|
||||
Reference in New Issue
Block a user