ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0085 Maximal Rectangle

2022-04-28 07:00:05  阅读:205  来源: 互联网

标签:Maximal matrix int res heights length 0085 LeetCode row


原题传送门

1. 题目描述

2. Solution 1

1、思路分析
以 example 1. 为例
matrix = [
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]

分析: 遍历 行(row)
row = 1: 可以得到 heights = [1, 0, 1, 0, 0] 套用 Q84
row = 2: -> heights = [2, 0, 2, 1, 1]
......

2、代码实现

package Q0099.Q0085MaximalRectangle;

import java.util.ArrayDeque;
import java.util.Deque;

/*
   以 example 1. 为例
    matrix = [
      ["1","0","1","0","0"],
      ["1","0","1","1","1"],
      ["1","1","1","1","1"],
      ["1","0","0","1","0"]
    ]

    分析: 遍历 行(row)
    row = 1: 可以得到 heights = [1, 0, 1, 0, 0] 套用 Q84
    row = 2: -> heights = [2, 0, 2, 1, 1]
    ......
  */
public class Solution1 {
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        int col = matrix[0].length;
        int[] heights = new int[col];
        int max = 0;
        for (char[] chars : matrix) {
            for (int j = 0; j < col; j++) {
                if (chars[j] == '1') {
                    heights[j]++;
                } else {
                    heights[j] = 0;
                }
            }
            int area = largestRectangleArea(heights);
            max = Math.max(max, area);
        }
        return max;
    }

    // 来自 Q84
    public int largestRectangleArea(int[] heights) {
        int len = heights.length;
        Deque<Integer> s = new ArrayDeque<>();
        int maxArea = 0;
        for (int i = 0; i <= len; i++) {
            int h = (i == len ? 0 : heights[i]);
            if (s.isEmpty() || h >= heights[s.peek()]) {
                s.push(i);
            } else {
                int tp = s.pop();
                int width = (s.isEmpty() ? i : i - 1 - s.peek());
                maxArea = Math.max(maxArea, heights[tp] * width);
                i--;
            }
        }
        return maxArea;
    }
}

3、复杂度分析
时间复杂度: O(m * n) 其中 dim(matrix) = (m, n)
空间复杂度: O(n) 栈

2. Solution 2

1、思路分析
思路同Solution 1,用数组模拟栈。
2、代码实现

package Q0099.Q0085MaximalRectangle;

public class Solution2 {
    /*
    执行耗时:2 ms,击败了100.00% 的Java用户
    内存消耗:41.8 MB,击败了36.24% 的Java用户
    */
    //方案2
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        int m = matrix.length;
        int n = matrix[0].length;
        int[] dp = new int[n + 1];//增加一列,简化边界处理
        int res = 0;
        for (int row = 0; row < m; row++) {
            updateHeight(dp, row, matrix);

            res = Math.max(res, maxArea(dp));
        }
        return res;
    }

    public void updateHeight(int[] dp, int row, char[][] matrix) {
        for (int column = 0; column < matrix[row].length; column++) {
            dp[column] = matrix[row][column] == '1' ? dp[column] + 1 : 0;
        }
    }

    public int maxArea(int[] heights) {
        int l = 0, r = 0;
        int n = heights.length;
        int[] pos = new int[n + 1];
        pos[l] = -1;
        int res = 0;
        while (r < n) {
            while (l > 0 && heights[r] < heights[pos[l]]) {
                int h = heights[pos[l--]];
                res = Math.max(res, h * (r - pos[l] - 1));
            }
            pos[++l] = r++;
        }
        return res;
    }
}

3、复杂度分析
时间复杂度: O(m * n) 其中 dim(matrix) = (m, n)
空间复杂度: O(n) 栈

标签:Maximal,matrix,int,res,heights,length,0085,LeetCode,row
来源: https://www.cnblogs.com/junstat/p/16201379.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有