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 最大矩形和的更多相关文章

  1. LeetCode:矩形区域【223】

    LeetCode:矩形区域[223] 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. 示例: 输入: -3, 0, 3, 4, ...

  2. LeetCode子矩形查询

    LeetCode 子矩形查询 题目描述 请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作: upda ...

  3. LeetCode 223. 矩形面积(Rectangle Area)

    223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...

  4. Leetcode 363.矩形区域不超过k的最大数值和

    矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...

  5. [LeetCode] 223.矩形面积

    题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...

  6. LeetCode 836. 矩形重叠

    题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左 ...

  7. Java实现 LeetCode 836 矩形重叠(暴力)

    836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...

  8. Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和

    363. 矩形区域不超过 K 的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,- ...

  9. Java实现 LeetCode 223 矩形面积

    223. 矩形面积 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. Rectangle Area 示例: 输入: -3, 0, 3, 4 ...

随机推荐

  1. Android MVP模式的初识

      MVP是什么?或许在之前更多的人知道的是MVC这个模式(Model View Controller),然而MVP与MVC最不同的一点是M与V是不直接 关联的也是就Model与View不存在直接关系 ...

  2. 建立一个方法的attribute,可以放在任意方法上,可以自动记录方法出错时的信息,就不用写try 。。cacth. 【注意】 不是在asp.net MVC下,是在普通三层结构下写的的特性。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  3. MVC权限模块

    设计方向: 1.摒弃SiteMap,避免在容易书写错误的sitemap中书写,导航在controller和action上打标签生成. 2.controller统一继承basecontroller,在b ...

  4. 对N个数组进行操作。先把这N个一维数组合并成一个2为数组;然后进行操作

    using System;using System.Collections.Generic;using System.Linq;using System.Collections;using Syste ...

  5. Linux下安装gcc 、g++ 、gfortran编译器

    一.ubuntu下gcc/g++/gfortran的安装 1.安装 (1).gcc ubuntu下自带gcc编译器.可以通过“gcc -v”命令来查看是否安装. (2).g++ 安装g++编译器,可以 ...

  6. 将VIM配置成强大的IDE(二)

    将VIM配置成强大的IDE(二) 前面我们已经安装好了vundle这一款强大的插件管理工具. 下面,当然是配置我们需要的插件了. 在VIM下面通过命令 help vundle 我们可以知道,VUNDL ...

  7. PHPCMS标签:get标签

    GET标签源自于PHPCMS 2008版,其使用SQL语句直接获取数据的特性,成为大家制作模板的首选. 在V9中这样强大的工具也得到保留下来. GET标签使用方式如下: {pc:get sql=&qu ...

  8. ASP.NET 后台不识别ASPX中的控件

    请问后台不识别ASPX中的控件,怎么解决 这个程序是在网上下载的 C# code <asp:DataGrid runat="server" ID="dgList1& ...

  9. 练习2 E题 - 求奇数的乘积

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 给你n ...

  10. 几个模式识别和计算机视觉相关的Matlab工具箱

    模式识别.计算机视觉.图像处理等领域大部分是对一些图像等数据的处理,比较常用的语言是C++和Matlab,相应也对应很多库,象opencv等,都是很好用功能也很强大,但是对于数据处理更方便的应该还是M ...