leetcode 最大矩形和
1.枚举法(超时)
public class Solution {
public int largestRectangleArea(int[] height) {
int max=-1;
for(int i=0;i<height.length;i++)
{
int len=1;
int k=i-1;
while(k>=0&&height[k]<=height[i])
{
k--;
len++;
}
k=i+1;
while(k<height.length&&height[k]<=height[i])
{
len++;
k++;
}
if(max<len)
{
max=len;
}
}
return max;
}
}
2.单调栈(AC),其实模拟了第一种方法,nyoj做过,效率为线性。写的代码有点长,但是很好理解
class node
{
int d; //di
int h;// higtht
} public class Solution {
public int largestRectangleArea(int[] height) {
int len=height.length;
if(len==0) return 0;
node n[]=new node[len];
for(int i=0;i<len;i++)
{
n[i]=new node();
n[i].d=1;
n[i].h=height[i]; }
Stack<node> s=new Stack<node>();
s.push(n[0]);
int max=n[0].h;
for(int j=1;j<len;j++)
{
node t=s.peek();
if(n[j].h>=t.h)
{
s.push(n[j]);
}
else
{
int with=0;
while(!s.isEmpty()&&s.peek().h>n[j].h)
{
t=s.pop();
with+=t.d;
if(with*t.h>max) max=with*t.h; }
n[j].d+=with;
s.push(n[j]); } }
int with=0;
while(!s.isEmpty())
{
node t=s.pop();
with=with+t.d;
int temp=t.h*with; if(temp>max) max=temp; } return max;
}}
3.最大01矩阵()。使用上述四项,枚举每一行。
class node
{
int d; //di
int h;// higtht
} public class Solution {
public int largest(int[] height) {
int len=height.length;
if(len==0) return 0;
node n[]=new node[len];
for(int i=0;i<len;i++)
{
n[i]=new node();
n[i].d=1;
n[i].h=height[i]; }
Stack<node> s=new Stack<node>();
s.push(n[0]);
int max=n[0].h;
for(int j=1;j<len;j++)
{
node t=s.peek();
if(n[j].h>=t.h)
{
s.push(n[j]);
}
else
{
int with=0;
while(!s.isEmpty()&&s.peek().h>n[j].h)
{
t=s.pop();
with+=t.d;
if(with*t.h>max) max=with*t.h; }
n[j].d+=with;
s.push(n[j]); } }
int with=0;
while(!s.isEmpty())
{
node t=s.pop();
with=with+t.d;
int temp=t.h*with; if(temp>max) max=temp; } return max;
}
public int maximalRectangle(char[][] matrix) {
if(matrix.length==0) return 0; int len=matrix[0].length;
int ans[]=new int[len];
for(int i=0;i<len;i++)
{
ans[i]=matrix[0][i]-'0';
}
int max=largest(ans); for(int i=1;i<matrix.length;i++)
{
for(int j=0;j<matrix[0].length;j++)
{
if(matrix[i][j]=='0') ans[j]=0; else
{
ans[j]+=1;
} } int t=largest(ans);
if(max<t) max=t; } return max; }
}
https://oj.leetcode.com/problems/maximal-rectangle/
leetcode 最大矩形和的更多相关文章
- LeetCode:矩形区域【223】
LeetCode:矩形区域[223] 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. 示例: 输入: -3, 0, 3, 4, ...
- LeetCode子矩形查询
LeetCode 子矩形查询 题目描述 请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作: upda ...
- LeetCode 223. 矩形面积(Rectangle Area)
223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...
- Leetcode 363.矩形区域不超过k的最大数值和
矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...
- [LeetCode] 223.矩形面积
题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...
- LeetCode 836. 矩形重叠
题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左 ...
- Java实现 LeetCode 836 矩形重叠(暴力)
836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...
- Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和
363. 矩形区域不超过 K 的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,- ...
- Java实现 LeetCode 223 矩形面积
223. 矩形面积 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. Rectangle Area 示例: 输入: -3, 0, 3, 4 ...
随机推荐
- [功能帮助类] JsHelper--Javascript操作帮助类 (转载)
点击下载 JsHelper.rar 这个类是关于加密,解密的操作,文件的一些高级操作1.Javascript弹出信息,并跳转指定页面. 2.Javascript弹出信息,并返回历史页面3.Javasc ...
- .NET设计模式(9):桥接模式(Bridge Pattern)
.NET设计模式(9):桥接模式(Bridge Pattern) 桥接模式(Bridge Pattern) --.NET设计模式系列之九 年月 实现代码如下:..所谓抽象和实现沿着各自维度的变 ...
- windows 8 vpn 错误解决
最近微软发布了Windows 8 RTM版,很多朋友也安装了,我当然也不例外.这几天就有不少朋友问我VPN连接无论怎么都说密码错误不能验证,于是,便连接VPN进行了下测试,如下: 配置好VPN,步凑不 ...
- Object To Enum
public static T ObjectToEnum<T>(object o) { try { return (T)Enum.Parse(typeof(T), o.ToString() ...
- 跨域的小小总结:js跨域及跨域的几种解决方法
一.什么是跨域?? js跨域请求就是使用js访问iframe里的不同域名下的页面内容,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同的域的iframe框架中的数据.即只要域名.协议. ...
- java常用正则表达式
1.邮编 public static final String POSTAL_CODE = "^\\d{6}$"; 2. email(支持中文域名邮箱) 正则表达式 public ...
- Constant is not finite! That's illegal. constant:inf'
原本使用正常的情况, 切换为测试库突然出现这个错误, 网上搜索并排查后得出导致这个问题的原因: (1)就是你的除数为0(2)除数或者被除数为null 找出你出错的界面, 并打断点, 看看是否出现上面两 ...
- html-----005
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSS 背景
CSS 背景属性用于定义HTML元素的背景. CSS 属性定义背影效果: background-color background-image background-repeat background- ...
- 谷歌的C++智能指针实现
//智能指针基类所有智能指针对象都继承该类class RefCountedBase { public: ; ; protected: virtual ~RefCountedBase(){} }; 智能 ...