2018-09-15 10:23:44

一、Largest Rectangle in Histogram

在求解最大的矩形面积之前,我们先讨论一条最大直方图面积的问题。

问题描述:

问题求解:

解法一、朴素解法,O(n ^ 2)。

解决的思路就是遍历一遍,如果当前的数比后一个数要小,那么当前的额数字肯定不可能是最大面积的右边界,遍历下一个数;

如果当前数比后一个大,那么假设当前的为右边界,向左进行遍历,计算面积最大值。

  1. public int largestRectangleArea(int[] heights) {
  2. if (heights.length == 0) return 0;
  3. int res = 0;
  4. for (int i = 0; i < heights.length; i++) {
  5. if (i == heights.length - 1 || heights[i] > heights[i + 1]) {
  6. int minHeight = heights[i];
  7. for (int j = i; j >= 0; j--) {
  8. minHeight = Math.min(heights[j], minHeight);
  9. res = Math.max(res, minHeight * (i - j + 1));
  10. }
  11. }
  12. }
  13. return res;
  14. }

解法二、使用堆栈,时间复杂度O(n)。

如何更快的解决这个问题呢?这里需要从另一个角度来考虑这个问题,其实解法一也是一种类似DP的解法,它的核心思路就是固定最后一个数,来获得以当前数为结尾的最大矩形面积。其实还有另一个角度来思考,就是以每个数作为高度能获得的最大面积是多少?其实这个问题就是需要找当前数左右第一个比其低的数,然后就可以得出以当前数字为高度的最大矩形面积,最后我们只需要遍历比较一遍就可以得到最大的结果。

  1. public int largestRectangleArea(int[] nums) {
  2. int n = nums.length;
  3. int[] l = new int[n];
  4. int[] r = new int[n];
  5. Stack<Integer> stack = new Stack<>();
  6. for (int i = 0; i < nums.length; i++) {
  7. while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
  8. l[i] = stack.isEmpty() ? 0 : stack.peek() + 1;
  9. stack.push(i);
  10. }
  11. stack.clear();
  12. for (int i = nums.length - 1; i >= 0; i--) {
  13. while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
  14. r[i] = stack.isEmpty() ? nums.length - 1 : stack.peek() - 1;
  15. stack.push(i);
  16. }
  17. int res = 0;
  18. for (int i = 0; i < n; i++) {
  19. res = Math.max(res, nums[i] * (r[i] - l[i] + 1));
  20. }
  21. return res;
  22. }

二、Maximal Rectangle

问题描述:

问题求解:

有个上一个问题的铺垫,这个问题就很好解决了,针对每一行,可以先求出其高度,然后再对每一行求最大最方图的面积,取max即可。

使用一个height的二维数组进行高度的保存,可以将时间复杂度降到O(mn)

  1. public int maximalRectangle(char[][] matrix) {
  2. if (matrix.length == 0 || matrix[0].length == 0) return 0;
  3. int m = matrix.length;
  4. int n = matrix[0].length;
  5. int[][] height = new int[m][n];
  6. for (int i = 0; i < n; i++) if (matrix[0][i] == '1') height[0][i] = 1;
  7. for (int i = 1; i < m; i++) {
  8. for (int j = 0;j < n; j++) {
  9. if (matrix[i][j] == '0') height[i][j] = 0;
  10. else height[i][j] = 1 + height[i - 1][j];
  11. }
  12. }
  13. int res = 0;
  14. for (int i = 0; i < m; i++) {
  15. res = Math.max(res, helper(height[i]));
  16. }
  17. return res;
  18. }
  19.  
  20. private int helper(int[] nums) {
  21. int n = nums.length;
  22. int[] l = new int[n];
  23. int[] r = new int[n];
  24. Stack<Integer> stack = new Stack<>();
  25. for (int i = 0; i < nums.length; i++) {
  26. while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
  27. l[i] = stack.isEmpty() ? 0 : stack.peek() + 1;
  28. stack.push(i);
  29. }
  30. stack.clear();
  31. for (int i = nums.length - 1; i >= 0; i--) {
  32. while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
  33. r[i] = stack.isEmpty() ? nums.length - 1 : stack.peek() - 1;
  34. stack.push(i);
  35. }
  36. int res = 0;
  37. for (int i = 0; i < n; i++) {
  38. res = Math.max(res, nums[i] * (r[i] - l[i] + 1));
  39. }
  40. return res;
  41. }

最大的矩形面积 Maximal Rectangle的更多相关文章

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

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

  2. [Swift]LeetCode850. 矩形面积 II | Rectangle Area II

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

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

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

  4. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  5. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. [LeetCode] 85. Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  7. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  8. Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. [Swift]LeetCode223. 矩形面积 | Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

随机推荐

  1. Atlas读写分离[高可用]

    Atlas下载地址: https://github.com/Qihoo360/Atlas/releases Atlas是出于360的, 比mysql-proxy更稳定, 部署起来更方便. 环境: pr ...

  2. ajax返回数据

    在使用远程js验证检测账户是否存在时,直在发请求后返回值无效,怎样把值返回回来呢重点注意两点 第一点:type不能省略,不能是异步,async: false 第二点:不能在直接请求成功后返回 var ...

  3. topcoder srm 455 div1

    problem1 link 令$f[x1][y1][x2][y2]$表示矩形(x1,y1)(x2,y2)中能选出的最大值.dp即可. problem2 link 这个题应该有更好的递推公式. 我的做法 ...

  4. 对应web的常用flutter应用

    例如,创建一个Text widget: new Text('Hello World', style: new TextStyle(fontSize: 32.0)) 创建一个 Image widget: ...

  5. 大数乘法|2012年蓝桥杯B组题解析第六题-fishers

    (9')大数乘法 对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的 ...

  6. Golang踩坑录 两种方式来读取文件一行所导致的问题

    前两天零零碎碎看完了golang的基础,想着找个小项目练练手,可是出现了一个十分棘手的问题 我要做的东西是网站路径爆破 所以我会从文本字典中把一行行路径读取然后与域名拼接,但是我在跑起程序后出现了问题 ...

  7. 【重新分配分片】Elasticsearch通过reroute api重新分配分片

    elasticsearch可以通过reroute api来手动进行索引分片的分配. 不过要想完全手动,必须先把cluster.routing.allocation.disable_allocation ...

  8. 51nod 四级题 汇总

    51Nod-1060-最复杂的数 #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; ...

  9. 论文笔记:Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy Visual Tracking

    Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy Visual Tracking  本文目标在于 ...

  10. 常用处理数组、字符串API → forEach every some sort map filter slice split indexOf concat substring substr splice join toString replace

    Object与Array的语法糖 var arr = [1,2,3]; // [] 是 new Array(1,2,3) 的语法糖(简写) var obj = {'name':2,'age':3}; ...