LeetCode85 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. (Hard)
For example, given the following matrix:
1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0
Return 6.
分析:
这个题挺不好想的,可以先考虑暴力的做法,遍历所有节点作为可能有的长方形的左上角。
然后对于每个左上角,向下遍历每一行,维护更新一个各个行中向右最少的1的个数,直到不是1的行为止,这样可以计算一个长方形面积。
对于每个左上角计算一下,则可以得到最大长方形面积。
考虑这个做法的时间复杂度,应该是O(n^4)。
进一步优化,发现数组中向右或向下连续1的个数是一个计算问题的关键,考虑能不能实现用数组存好这一数据,再进行计算。
于是发现此题与前一题Largest Rectangle in Histogram有共通之处。
将dp数组存放以此节点开始向下的最长1的个数,则对于每一行,实际就是一个Largest Rectangle in Histogram问题。对于每一行调用上一问题即可。
代码:
1 class Solution { 2 private: 3 int largestRectangleArea(vector<int>& heights) { 4 int result = 0; 5 int sz = heights.size(); 6 stack<int> s; 7 for (int i = 0; i < sz; ++i) { 8 if (s.empty() || heights[i] >= heights[s.top()]) { 9 s.push(i); 10 } 11 else { 12 while (!s.empty() && heights[s.top()] > heights[i]) { 13 int cur = s.top(); 14 s.pop(); 15 int prev = (s.empty()) ? -1 : s.top(); 16 result = max(result, (i - prev - 1) * heights[cur] ); 17 } 18 s.push(i); 19 } 20 } 21 while (!s.empty()) { 22 int cur = s.top(); 23 s.pop(); 24 int prev = (s.empty()) ? -1 : s.top(); 25 result = max(result, (sz - prev - 1) * heights[cur] ); 26 } 27 return result; 28 } 29 public: 30 int maximalRectangle(vector<vector<char>>& matrix) { 31 if (matrix.size() == 0) { 32 return 0; 33 } 34 int rowSize = matrix.size(), colomnSize = matrix[0].size(); 35 vector<vector<int>> dp(rowSize, vector<int>(colomnSize)); 36 for (int i = 0; i < rowSize; ++i) { 37 for (int j = 0; j < colomnSize; ++j) { 38 dp[i][j] = 0; 39 for (int k = 0; i + k < rowSize && matrix[i + k][j] == '1'; ++k) { 40 dp[i][j]++; 41 } 42 } 43 } 44 int result = 0; 45 for (int i = 0; i < rowSize; ++i) { 46 result = max(result, largestRectangleArea(dp[i])); 47 } 48 return result; 49 } 50 };
转载于:https://www.cnblogs.com/wangxiaobao/p/6005562.html
LeetCode85 Maximal Rectangle相关推荐
- [LeetCode]Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【DP】LeetCode 85. Maximal Rectangle
LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...
- [Swift]LeetCode85. 最大矩形 | Maximal Rectangle
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)
题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用](最大矩形)
leetcode之Largest Rectangle in Histogram 标签: leetcode面试题最大矩形堆栈单调队列 2016-07-30 13:47 1325人阅读 评论(0) 收藏 ...
- 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵
1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- LeetCode 85. Maximal Rectangle --python,java解法
题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- Leetcode: Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- Leetcode | Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
最新文章
- DSP28335声音降噪(未完成)
- Git 安装和使用教程(Windows)
- ubuntu三种添加环境变量的方法
- _Linux自编译云锁Web网站防火墙,看完小白也能保护网站安全
- vue 和react
- scala 基础类库 —— 文件操作
- 取(2堆)石子游戏 (hdu2177)
- 华为云IoT体验:基于IoT平台构建智慧路灯应用
- IDEA汉化后设置无法打开
- C语言7大常见排序(详细图解)
- Codeforces 4D. Mysterious Present
- 解决Windows更新失败(0x8007000d)的详细方法
- 单节点 Elasticsearch 健康状态为 yellow 问题的解决
- 怎么把ogg音乐格式转换成mp3
- TVS二极管和稳压二极管的区别
- ibm服务器如何查看内存型号,ibmx3650m4不识别|认不出内存怎么办|内存安装规则要求...
- 使用springboot的banner给小伙伴输出一波月饼
- 数据结构:八大数据结构学习总篇章
- 团体天梯 L2-030 冰岛人 (25 分)(思路、测试点分析)
- 成都夏季招聘会IT行业缺口大!
热门文章
- “360行,行行转前端”:前端岗为什么这么火?
- C语言头文件一般以什么名称结尾,c语言书写规范.doc
- 有哪些网站是django开发的_网站模板建设和定制开发哪个好,有哪些区别?
- 如何优化java反射,如何有效地使用Java反射
- mysql范围条件_MySQL8.0之跳跃范围扫描
- 如何格式化电脑_如何将c盘格式化,垃圾文件太多,不知道怎么办
- html中的各种协议,html 中使用 wtai 协议
- 三年级计算机教案 渔舟唱晚,《渔舟唱晚》大班教案
- NYOJ-超级台阶(dp)
- pyqt5使用label显示图片