84题和85五题 基本是一样的,先说84题

84--柱状图中最大的矩形( Largest Rectangle in Histogram)



思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度len,最后把len*heights[i] 就是延展开的面积,最后做比对,得出最大。

    public int largestRectangleArea(int[] heights) {
int ans=0;
for(int i=0;i<heights.length;i++) {
int len=1,left=i,right=i;
while(left>0&&heights[--left]>=heights[i]) len++;//向左延展
while(right<heights.length-1&&heights[++right]>=heights[i]) len++; //向右延展
ans=Math.max(ans,heights[i]*len); //更新最大值
}
return ans;
}

这个方法效率不是很高,但是是最简单,也是最容易理解的。、

85--最大矩形(Maximal Rectangle)

这一题的思路与上一题相同。主要是第一步。

首先我们先建立一个大小和Matrix相同的大小的二维数组map

        int Y=matrix.length;
int X=matrix[0].length;
int[][] map=new int[Y][X];
for(int y=0;y<Y;y++) {
for(int x=0;x<X;x++) {
if(matrix[y][x]=='1') {
/*假如matrix[y][x]=='1',就把当前位置的map值,加上map[y-1][x]的值,如
000 000
111 -》 111
101 202*/
if(y>0) {
map[y][x]=map[y-1][x]+1;
}else {
map[y][x]=1;
}
}
}
}

通过向下累加的方式,更新map,然后在对每一行的map[y],进行如84题的操作,就可以得出最大的面积

    public int maximalRectangle(char[][] matrix) {
if(matrix.length==0||matrix[0].length==0) return 0;
int Y=matrix.length;
int X=matrix[0].length;
int[][] map=new int[Y][X];
for(int y=0;y<Y;y++) {
for(int x=0;x<X;x++) {
if(matrix[y][x]=='1') {
if(y>0) {
map[y][x]=map[y-1][x]+1;
}else {
map[y][x]=1;
}
}
}
}
int ans=0;
//这里和84题一样
for(int[] nums:map) {
for(int i=0;i<nums.length;i++) {
int len=1,left=i,right=i;
while(left>0&&nums[--left]>=nums[i]) len++;
while(right<nums.length-1&&nums[++right]>=nums[i]) len++;
ans=Math.max(ans,nums[i]*len);
}
}
return ans;
}

就是这样

LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)的更多相关文章

  1. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...

  2. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram

  3. Java实现 LeetCode 84 柱状图中最大得矩形

    84. 柱状图中最大的矩形 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的 ...

  4. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

  6. leetcode 84. 柱状图中最大的矩形 JAVA

    题目: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高 ...

  7. [LeetCode] 84. 柱状图中最大的矩形

    题目链接 : https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱 ...

  8. 73.Largest Rectangle in Histogram(最大矩形)

    Level:   Hard 题目描述: Given n non-negative integers representing the histogram's bar height where the ...

  9. LeetCode 84 | 单调栈解决最大矩形问题

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第52篇文章,我们一起来看LeetCode第84题,Largest Rectangle in Histogram( ...

  10. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

随机推荐

  1. 夯实Java基础系列4:一文了解final关键字的特性、使用方法,以及实现原理

    目录 final使用 final变量 final修饰基本数据类型变量和引用 final类 final关键字的知识点 final关键字的最佳实践 final的用法 关于空白final final内存分配 ...

  2. Android之SOAP协议与WebService服务器交互,解决超时的问题

    网络搜索大部分不能实际解决问题.特意将解决方法写下.创建MyAndroidHttpTransport 类 , package com.example.omhandroid.lib; import or ...

  3. Dubbo学习系列之九(Shiro+JWT权限管理)

    村长让小王给村里各系统来一套SSO方案做整合,隔壁的陈家村流行使用Session+认证中心方法,但小王想尝试点新鲜的,于是想到了JWT方案,那JWT是啥呢?JavaWebToken简称JWT,就是一个 ...

  4. hbase、pig、hive配置与应用

    ------------------HBASE---------- [root@iClient~]#sudo yum install hbase #iClient安装Hbase客户端 [root@cM ...

  5. Angular6 CodeMirror在线编辑sql 智能提示

    1. 安装ng2-codemirror包.codemirror包 npm install ng2-codemirror -- save npm install codemirror -- save 2 ...

  6. jQuery常用方法(四)-选择器

    JQuery Selectors 方法说明 基本选择器 $("#myDiv") 匹配唯一的具有此id值的元素 $("div") 匹配指定名称的所有元素 $(&q ...

  7. JQuery入门学习笔记(全)

    jQuery 语法 $(this).hide() - 隐藏当前元素 $("p").hide() - 隐藏所有 元素 $("p.test").hide() - 隐 ...

  8. Scala 学习笔记之函数(2)

    class OldStudent extends Student { def filterName(s: String, f: String => String) = { if (s != nu ...

  9. .Net Core下使用HtmlAgilityPack解析采集互联网数据

    HtmlAgilityPack应该算是.Net下最好用的html解析库了. 因为最近帮朋友采集一些数据,在nuget里面搜索了好几个库,最后决定就用HtmlAgilityPack.并简单的记录下使用的 ...

  10. 我又不是你的谁--java instanceof操作符用法揭秘

    背景故事 <曾经最美>是朱铭捷演唱的一首歌曲,由陈佳明填词,叶良俊谱曲,是电视剧<水晶之恋>的主题曲.歌曲时长4分28秒. 歌曲歌词: 看不穿你的眼睛 藏有多少悲和喜 像冰雪细 ...