Leetcode 85. Maximal Rectangle (Hard) (cpp)
来源:互联网 发布:泳衣淘宝网 编辑:程序博客网 时间:2024/06/06 10:54
Leetcode 85. Maximal Rectangle (Hard) (cpp)
Tag: Array, Hash Table, Stack, Dynamic Programming
Difficulty: Hard
/*85. Maximal Rectangle (Hard)Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return 6.*/class Solution {public:int maximalRectangle(vector<vector<char>>& matrix) {if (matrix.empty()) {return 0;}int maxRec = 0;vector<int> height(matrix[0].size());for (int i = 0; i < matrix.size(); i++) {for (int j = 0; j < matrix[0].size(); j++) {height[j] = matrix[i][j] == '0' ? 0 : height[j] + 1;}maxRec = max(maxRec, largestRectangleArea(height));}return maxRec;}private:int largestRectangleArea(const vector<int>& heights) {stack<int> s;int maxSize = 0;for (int i = 0; i <= heights.size(); i++) {int cur = i == heights.size() ? 0 : heights[i];while (!s.empty() && heights[s.top()] >= cur) {int height = heights[s.top()];s.pop();int left = s.empty() ? 0 : s.top() + 1;maxSize = max(maxSize, (i - left) * height);}s.push(i);}return maxSize;}};
0 0
- Leetcode 85. Maximal Rectangle (Hard) (cpp)
- LeetCode题目85.Maximal Rectangle(Hard)
- 85. Maximal Rectangle (leetcode hard)
- LeetCode 85. Maximal Rectangle(Hard)
- LeetCode 85. Maximal Rectangle
- [Leetcode] 85. Maximal Rectangle
- LeetCode --- 85. Maximal Rectangle
- [LeetCode]*85.Maximal Rectangle
- [leetcode] 85.Maximal Rectangle
- Leetcode 85. Maximal Rectangle
- leetcode 85. Maximal Rectangle
- LeetCode 85. Maximal Rectangle
- Leetcode 85. Maximal Rectangle
- Leetcode:85. Maximal Rectangle
- LeetCode 85. Maximal Rectangle
- LeetCode-85.Maximal Rectangle
- leetcode.85. Maximal Rectangle
- leetcode 85. Maximal Rectangle
- 云计算复习笔记一
- 阿里云直播服务 sdk demo php
- 安装chardet 问题
- mysql 5.7 版本后数据没有字段 password 所以修改 root 密码 需要
- PostgreSQL表空间创建、删除
- Leetcode 85. Maximal Rectangle (Hard) (cpp)
- C#多线程同步(二)【Mutex】
- 公共子序列(lcs)
- 连续合数探求
- shell使用命令计算md5
- Android studio 图片加载框架Glide介绍及使用
- 修改centos 7.0 的yum源优先级priority
- ArcGIS Engine控件运行许可学习总结
- The `master` repo requires CocoaPods 1.0.0 - (currently using 0.39.0)