1.枚举法(超时)

  1. public class Solution {
  2. public int largestRectangleArea(int[] height) {
  3. int max=-1;
  4. for(int i=0;i<height.length;i++)
  5. {
  6. int len=1;
  7. int k=i-1;
  8. while(k>=0&&height[k]<=height[i])
  9. {
  10. k--;
  11. len++;
  12. }
  13. k=i+1;
  14. while(k<height.length&&height[k]<=height[i])
  15. {
  16. len++;
  17. k++;
  18.  
  19. }
  20. if(max<len)
  21. {
  22. max=len;
  23. }
  24.  
  25. }
  26. return max;
  27.  
  28. }
  29. }

2.单调栈(AC),其实模拟了第一种方法,nyoj做过,效率为线性。写的代码有点长,但是很好理解

  1. class node
  2. {
  3. int d; //di
  4. int h;// higtht
  5. }
  6.  
  7. public class Solution {
  8. public int largestRectangleArea(int[] height) {
  9. int len=height.length;
  10. if(len==0) return 0;
  11. node n[]=new node[len];
  12. for(int i=0;i<len;i++)
  13. {
  14. n[i]=new node();
  15. n[i].d=1;
  16. n[i].h=height[i];
  17.  
  18. }
  19. Stack<node> s=new Stack<node>();
  20. s.push(n[0]);
  21. int max=n[0].h;
  22. for(int j=1;j<len;j++)
  23. {
  24. node t=s.peek();
  25. if(n[j].h>=t.h)
  26. {
  27. s.push(n[j]);
  28. }
  29. else
  30. {
  31. int with=0;
  32. while(!s.isEmpty()&&s.peek().h>n[j].h)
  33. {
  34. t=s.pop();
  35. with+=t.d;
  36. if(with*t.h>max) max=with*t.h;
  37.  
  38. }
  39. n[j].d+=with;
  40. s.push(n[j]);
  41.  
  42. }
  43.  
  44. }
  45. int with=0;
  46. while(!s.isEmpty())
  47. {
  48. node t=s.pop();
  49. with=with+t.d;
  50. int temp=t.h*with;
  51.  
  52. if(temp>max) max=temp;
  53.  
  54. }
  55.  
  56. return max;
  57. }}

3.最大01矩阵()。使用上述四项,枚举每一行。

  1. class node
  2. {
  3. int d; //di
  4. int h;// higtht
  5. }
  6.  
  7. public class Solution {
  8. public int largest(int[] height) {
  9. int len=height.length;
  10. if(len==0) return 0;
  11. node n[]=new node[len];
  12. for(int i=0;i<len;i++)
  13. {
  14. n[i]=new node();
  15. n[i].d=1;
  16. n[i].h=height[i];
  17.  
  18. }
  19. Stack<node> s=new Stack<node>();
  20. s.push(n[0]);
  21. int max=n[0].h;
  22. for(int j=1;j<len;j++)
  23. {
  24. node t=s.peek();
  25. if(n[j].h>=t.h)
  26. {
  27. s.push(n[j]);
  28. }
  29. else
  30. {
  31. int with=0;
  32. while(!s.isEmpty()&&s.peek().h>n[j].h)
  33. {
  34. t=s.pop();
  35. with+=t.d;
  36. if(with*t.h>max) max=with*t.h;
  37.  
  38. }
  39. n[j].d+=with;
  40. s.push(n[j]);
  41.  
  42. }
  43.  
  44. }
  45. int with=0;
  46. while(!s.isEmpty())
  47. {
  48. node t=s.pop();
  49. with=with+t.d;
  50. int temp=t.h*with;
  51.  
  52. if(temp>max) max=temp;
  53.  
  54. }
  55.  
  56. return max;
  57. }
  58. public int maximalRectangle(char[][] matrix) {
  59. if(matrix.length==0) return 0;
  60.  
  61. int len=matrix[0].length;
  62. int ans[]=new int[len];
  63. for(int i=0;i<len;i++)
  64. {
  65. ans[i]=matrix[0][i]-'0';
  66. }
  67. int max=largest(ans);
  68.  
  69. for(int i=1;i<matrix.length;i++)
  70. {
  71. for(int j=0;j<matrix[0].length;j++)
  72. {
  73. if(matrix[i][j]=='0') ans[j]=0;
  74.  
  75. else
  76. {
  77. ans[j]+=1;
  78. }
  79.  
  80. }
  81.  
  82. int t=largest(ans);
  83. if(max<t) max=t;
  84.  
  85. }
  86.  
  87. return max;
  88.  
  89. }
  90. }

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. Bootstrap: 样式CSS:carousel轮换 图片的使用

    Bootstrap 轮播(Carousel)插件 Bootstrap轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除此之外,内容也是足够灵活的,可以是图像.内嵌框架.视频或者其 ...

  2. MyEclipse中配置自己的JRE和tomcat

    MyEclipse中配置自己的JRE:windows>Preference>java>Installed JREs>Add>Stantard VM>next> ...

  3. IOS-UIScrollView实现图片分页

    1.设置可以分页 _scrollView.pagingEnabled = YES; 2.添加PageControl UIPageControl *pageControl = [[UIPageContr ...

  4. vecor预分配内存溢出2

    vector预分配内存溢出导致原始的 迭代器 失效 consider what happens when you add the one additional object that causes t ...

  5. HDU1557权利选举

    /* 思路:遍历所有2^n个集合,对于每个集合求票和,如果满足票为优胜团体,而再对集合每个成员比较,是否满足变成非优胜团体,是的话,对于该成员对应结果+1. 重点:利用二进制思想,所有团体均对应0~2 ...

  6. 转载C#泛型集合—Dictionary<K,V>使用技巧

    1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...

  7. [转] 属性选择器.mark

    CSS 2 引入了属性选择器. 属性选择器可以根据元素的属性及属性值来选择元素. 简单属性选择 如果希望选择有某个属性的元素,而不论属性值是什么,可以使用简单属性选择器. 例子 1 如果您希望把包含标 ...

  8. 存储过程修改产品描述页图片alt描述信息

    今天修改了所有产品的图片信息,用到了存储过程.在参考下面存储过程以后,终于搞定了. 1 BEGIN 2 DECLARE Done INT DEFAULT 0; 3 4 DECLARE CurrentL ...

  9. #Leet Code# Root to leaf

    语言:Python 描述:使用递归实现 def getList(self, node): if node is None: return [] if node.left is None and nod ...

  10. 关于applicationx/www-form-urlencoded和multipart/form-data的描述

    在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...