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 ...
随机推荐
- [dp]最长单调递增子序列LIS
https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...
- C#中的new修饰符说明
new修饰符主要是用来隐藏从基类继承的成员. 这句话怎么理解呢,就是说有一个类,它有一个继承类,继承类中存在和基类中一样名称的成员(属性,方法等). 对继承类中的该成员使用new修饰符时,调用时将会隐 ...
- HDU 3966 Aragorn's Story (简单树链剖分)
题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有点权值减去K Q C:查询节点编号为C ...
- FUI- 我离钢铁侠还差几步?
本文来自网易云社区 作者:马宝 什么是FUI本文不累赘的可以自行Google,喜欢科幻的同学们都看一张图就能感受到FUI的魅力. 本文算是一篇所见即所的,可边学边干的原创教程.总结全文就一句话,&qu ...
- Vue.js 源码实现
目录 Vue.js 代码实现 1. 步骤一 2. 步骤二 3.步骤三 Vue.js 工作机制 初始化 编译 响应式 虚拟dom 更新视图 编译 Vue.js 代码实现 检验学习效果的最好方法就是自己造 ...
- 读《JavaScript权威指南》笔记(二)
1.加号运算和比较运算符的区别 对于数字和字符串操作符来说,加号运算符和比较运算符的行为都有所不同,前者更偏爱字符串,如果它的其中一个操作数是字符串的话,则进行字符串连接操作.而比较运算符则更偏爱数字 ...
- 依托http-headers的 sql注入和时间盲注
机缘巧合接触了一点关于sql注入的网络安全问题 依托 headers 的 sql 注入 一般来说大家都很清楚用户输入的危险性,通常会对用户表单提交的数据进行过滤(引号转码). 但是如果写过网络爬虫,那 ...
- .NET ToString() format格式化字符串(常用)
前言 我们平常会用到货币数据类型,尤其当我们计算金钱或者算数的时候经常会遇到保留几位小数,而且碰到日期格式问题的时候,经常不知道选择什么样的格式比较合适,下面我找了一部分常用的.NET ToStrin ...
- DNS解析工具--nslookup和dig使用
1.nslookup使用 [root@master ~]# nslookup> server 8.8.8.8 #指定域名服务器Default server: 8.8.8.8Address: ...
- mysql之SQL入门与提升(二)
在mysql之SQL入门与提升(一)我们已经有了些许基础,今天继续深化 先造表 SET NAMES utf8;SET FOREIGN_KEY_CHECKS = 0; -- -------------- ...