leetcode85 Maximal Rectangle
思路:
分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算。
实现:
#include <bits/stdc++.h>
using namespace std; class Solution
{
public:
int maximalRectangle(vector<vector<char>>& matrix)
{
int n = matrix.size(); if (n == ) return ;
int m = matrix[].size(); if (m == ) return ;
int ans = ;
vector<int> h(m, ), l(m, ), r(m, );
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
h[j] = (matrix[i][j] == '' ? : h[j] + );
}
stack<int> s;
for (int j = ; j < m; j++)
{
while (!s.empty() && h[s.top()] >= h[j]) s.pop();
l[j] = (s.empty() ? : s.top() + );
s.push(j);
}
while (!s.empty()) s.pop();
for (int j = m - ; j >= ; j--)
{
while (!s.empty() && h[s.top()] >= h[j]) s.pop();
r[j] = (s.empty() ? m - : s.top() - );
s.push(j);
}
for (int j = ; j < m; j++)
{
ans = max(ans, (r[j] - l[j] + ) * h[j]);
}
}
return ans;
}
};
int main()
{
char a[][] = {{'','','','',''},
{'','','','',''},
{'','','','',''},
{'','','','',''}};
// char a[][4] = {{'1','1','1','1'},
// {'1','1','1','1'},
// {'1','1','1','1'}};
vector<vector<char>> v;
for (int i = ; i < ; i++)
{
v.push_back(vector<char>(a[i], a[i] + ));
}
cout << Solution().maximalRectangle(v) << endl;
return ;
}
leetcode85 Maximal Rectangle的更多相关文章
- LeetCode85 Maximal Rectangle java题解
public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
随机推荐
- [原创]SQL表值函数:把用逗号分隔的字符串转换成表格数据
我们日常开发过程中,非常常见的一种需求,把某一个用逗号或者/或者其他符号作为间隔的字符串分隔成一张表数据. 在前面我们介绍了 [原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式,当然按照这 ...
- 项目一:第七天 CRM 和bos系统实现定区关联客户,关联快递员. 通过CXF框架实现
定区关联客户 需求:为了快递方便客户下订单(发快递),派快递员上门取件. 所以说需要让定区关联客户(知道客户属于哪个定区),定区跟快递员关系:多对多.知道让哪个快递员上门取件. 将CRM系统中,客户 ...
- 2、Tophat align_summary.txt and samtools flagstat accepted_hits.bam disagree
###https://www.biostars.org/p/195758/ Left reads: Input : 49801387 Mapped : 46258301 (92.9% of input ...
- 《精通Spring4.X企业应用开发实战》读后感第五章(基于注解的配置)
- Appium 在 Android UI 测试中的应用
原文地址:https://blog.coding.net/blog/Appium-Android-UI Android 测试工具与 Appium 简介 Appium 是一个 C/S 架构的,支持 An ...
- Eclipse提交svn错误svn E210003 connection refused by the server
错误明细: org.apache.subversion.javahl.ClientException: svn: E210003: connection refused by the server o ...
- 为什么源码中很多方法就一行throw new RuntimeException("Stub!")
在使用某些类的方法时,发现其内部就一行throw new RuntimeException("Stub!"),但是实际运行中并没有抛出该错误,该方法也并没有语法报错. 因此可能是系 ...
- Unity技术支持团队性能优化经验分享
https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247490321&idx=1&sn=f9f34407ee5c5d ...
- cf835(预处理 + 记忆化dp)
题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...
- 浮点数与快速log2
请先于浮点数的文章:http://blog.jobbole.com/86371/ 先贴一张关于float和double的图: float: double: 快速log2长这样: int flog2(f ...