Skip to content

Commit

Permalink
feat: 语言优化
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifer committed Apr 6, 2021
1 parent 5a0d167 commit 72e115d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
2 changes: 1 addition & 1 deletion problems/84.largest-rectangle-in-histogram.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ https://leetcode-cn.com/problems/largest-rectangle-in-histogram/
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx8sr4uj305805odfn.jpg)

以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为  [2,1,5,6,2,3]


![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx9kgd2j305805oa9z.jpg)

图中阴影部分为所能勾勒出的最大矩形面积,其面积为  10  个单位。
Expand Down
39 changes: 5 additions & 34 deletions problems/85.maximal-rectangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ https://leetcode-cn.com/problems/maximal-rectangle/

## 代码

代码支持:Python, CPP
代码支持:Python

Python Code:

Expand Down Expand Up @@ -88,42 +88,13 @@ class Solution:

```

CPP Code:

```cpp
class Solution {
public:
int maximalRectangle(vector<vector<char>>& A) {
if (A.empty() || A[0].empty()) return 0;
int ans = 0, M = A.size(), N = A[0].size();
vector<int> left(N, 0), right(N, N), height(N, 0);
for (int i = 0; i < M; ++i) {
int curLeft = 0, curRight = N;
for (int j = 0; j < N; ++j) height[j] = A[i][j] == '1' ? height[j] + 1 : 0;
for (int j = 0; j < N; ++j) {
if (A[i][j] == '1') left[j] = max(left[j], curLeft);
else {
left[j] = 0;
curLeft = j + 1;
}
}
for (int j = N - 1; j >= 0; --j) {
if (A[i][j] == '1') right[j] = min(right[j], curRight);
else {
right[j] = N;
curRight = j;
}
}
for (int j = 0; j < N; ++j) ans = max(ans, (right[j] - left[j]) * height[j]);
}
return ans;
}
};
```
**复杂度分析**

- 时间复杂度:$O(M * N)$
- 空间复杂度:$O(N)$

以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 38K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。

```
```

0 comments on commit 72e115d

Please sign in to comment.