84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1.
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
For example,
Given heights = [2,1,5,6,2,3]
,
return 10
.
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
if( == n)
return ;
int max = , area, i, k;
stack<int> s;
heights.push_back();
for(i = ; i <= n; i++)
{
if(s.empty() || heights[i] >= heights[s.top()])
{
s.push(i);
continue;
}
k = s.top();
s.pop();
area = heights[k] * ( == s.size() ? i : i - s.top() - );
if(area > max)
max = area;
i--;
}
return max;
}
};
// As we know, the area = width * height
// For every bar, the 'height' is determined by the loweset bar.
//
// 1) We traverse all bars from left to right, maintain a stack of bars. Every bar is pushed to stack once.
// 2) A bar is popped from stack when a bar of smaller height is seen.
// 3) When a bar is popped, we calculate the area with the popped bar as smallest bar.
// 4) How do we get left and right indexes of the popped bar –
// the current index tells us the ‘right index’ and index of previous item in stack is the ‘left index’.
//
//
// In other word, the stack only stores the incresing bars, let's see some example
//
// Example 1
// ---------
// height = [1,2,3,4]
//
// stack[] = [ 0, 1, 2, 3 ], i=4
//
// 1) pop 3, area = height[3] * 1 = 4
// 2) pop 2, area = height[2] * 2 = 4
// 3) pop 1, area = height[1] * 3 = 6
// 4) pop 0, area = height[0] * 4 = 4
//
//
// Example 2
// ---------
// height = [2,1,2]
//
// stack[] = [ 0 ], i=1
// 1) pop 0, area = height[0] * 1 = 2
//
// stack[] = [ 1,2 ], i=3, meet the end
// 1) pop 2, area = height[2] * 1 = 2
// 2) pop 1, area = height[1] * 3 = 3
//
//
// Example 3
// ---------
// height = [4,2,0,3,2,5]
//
// stack[] = [ 0 ], i=1, height[1] goes down
// 1) pop 0, area = height[0] * 1 = 4
//
// stack[] = [ 1 ], i=2, height[2] goes down
// 1) pop 1, area = height[1] * 2 = 4 // <- how do we know the left?
// start from the 0 ??
//
// stack[] = [ 2, 3 ], i=4, height[4] goes down
// 1) pop 3, area = height[3] * 1 = 3
// 2) pop 2, area = height[2] * ? = 0 // <- how do we know the left?
// start from the 0 ??
//
// stack[] = [ 2,4,5 ], i=6, meet the end
// 1) pop 5, area = height[5] * 1 = 5
// 2) pop 4, area = height[4] * 3 = 6 // <- how do we know the left?
// need check the previous item.
// 3) pop 2, area = height[2] * ? = 4 // <- how do we know the left?
// start from the 0 ??
//
// so, we can see, when the stack pop the top, the area formular is
//
// height[stack_pop] * i - stack[current_top] - 1, if stack is not empty
// height[stack_pop] * i, if stack is empty
2.
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
class Solution {
public:
int maxRecArea(vector<int> heights)
{
stack<int> s;
int n = heights.size(), max = , area, i, k;
heights.push_back();
for(i = ; i <= n; i++)
{
if(s.empty() || heights[i] >= heights[s.top()])
{
s.push(i);
continue;
}
k = s.top();
s.pop();
area = heights[k] * (s.empty() ? i : i - s.top() - );
if(area > max)
max = area;
i--;
}
return max;
} int maximalRectangle(vector<vector<char>>& matrix) {
int m = matrix.size();
if( == m)
return ;
int n = matrix[].size(), area, max = , i, j;
vector<vector<int>> heights(m, vector<int>(n, ));
for(i = ; i < m; i++)
{
for(j = ; j < n; j++)
{
if('' == matrix[i][j])
heights[i][j] = ( == i ? : heights[i-][j]+);
}
area = maxRecArea(heights[i]);
if(area > max)
max = area;
}
return max;
}
};
// The problem can be convert to the problem - "Largest Rectangle in Histogram"
// 1) we can take each row to calculate each row's histogram.
// 2) using the algorithm of "Largest Rectangle in Histogram" to find the largest area histogram.
// 3) tracking the maximal area.
//
// For the 1), it's easy.
// heights[i][j] = 1, if (i==0)
// heights[i][j] = heights[i-1][j] + 1;, if (i>0)
//
// For the 2), please referr to "Largest Rectangle in Histogram"
84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形的更多相关文章
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 85. Maximal Rectangle (JAVA)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 85. Maximal Rectangle (Graph; Stack, DP)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- (三)github之GIT的导入
什么是版本管理? git是一款[分散型版本管理]软件,提供了开发过程中不可少的功能,例如记录一款软件添加或者更改源代码的过程,回溯到特定阶段,恢复误删除的文件等. 集中型:将仓库集中放在服务器中,一旦 ...
- Web负载均衡学习笔记之四层和七层负载均衡的区别
0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...
- ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_00163'
早上一运维同事说,一个报盘程序启动的时候报了"ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_ ...
- django multidb --- router
之前一篇随笔, 提到了django中怎么使用多数据库, 但是在实际工程中遇到了一个问题,就是admin指定了使用某库, 在测试环境上没问题, 当部署后(库也变动了位置), 修改一个admin的mode ...
- CSAPP 第三章 读书笔记
程序的机器级表示 AT&T与Intel格式的汇编代码 我们的表述是ATT(根据"AT&T"命名的, AT&T是运营贝尔实验室多年的公 司)格式的汇编代码,这 ...
- 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 1 章 答案
判断对错1.计算机科学是计算机的研究.2.CPU 是计算机的“大脑”.3.辅助存储器也称为 RAM.4.计算机当前正在处理的所有信息都存储在主存储器中.5.语言的语法是它的意思,语义是它的形式.6.函 ...
- MAKEFILE 编程基础之一【转】
本文转载自:http://www.himigame.com/gcc-makefile/766.html 概述: 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Wind ...
- js 注意点
1.var // 反例 myname = "global"; // 全局变量 function func() { alert(myname); // "undefined ...
- 51NOD 1046 A^B Mod C
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^9) ...
- HDU 6070 Dirt Ratio(分数规划+线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 找出一个区间,使得(区间内不同数的个数/区间长度)的值最小,并输出该值. 思路: 因为是要求$\f ...