Leetcode 221 & 85

来源:互联网 发布:手机人体建模软件 编辑:程序博客网 时间:2024/06/05 05:10

Leetcode 221 & 85

Leetcode 221 Maximal Square

Description

Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.For example, given the following matrix:

10100101111111110010

Return 4.

题意分析

这道题跟那道算猜数字的游戏比较像,用动态规划的方法比较容易。用一个数组dp[i][j]来存储以该点为右下角的最大正方形的面积大小,记录着面积大小,核心算法是dp[i][j] = min(dp[i - 1][j] + min(dp[i - 1][j - 1] + dp[i][j -1])) + 1,具体代码如下:

class Solution {public:    int maximalSquare(vector<vector<char>>& matrix) {        int rows = matrix.size();        if (rows <= 0)            return 0;        int cols = matrix[0].size(), max = INT_MIN;        vector<vector<int>> dp(rows + 1, vector<int>(cols + 1, 0));        for (int i = 1; i <= rows; i++) {            for (int j = 1; j <= cols; j++) {                // 为1的时候进行判断                if (matrix[i - 1][j - 1] == '1') {                    dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;                    if (max < dp[i][j])                        max = dp[i][j];                }            }        }        return max * max;    }};

Leetcode 85 Maximal Rectangle

Description

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
For example, given the following matrix:

10100101111111110010

Return 6.

题意分析

这道题看起来和上一道221求最大的正方形的类似,都是求一个最大的面积,但是想想如果按照221的方法去求的话如果判断矩形的长宽就需要花费很多功夫,所以需要想下其他的办法。其实这道题想想和84.Largest Rectangle in Histogram很像,只需要把矩阵的1转化成多个矩形,就可以用第84题的方法去做。然后如果一个1的上方也是1,那么就可以变成一个连接的矩形,然后依次得到对应的高度的矩形。矩阵的每一行对应一组矩形,比如例子的矩阵就对应下面的矩形:

10100202113132240030

然后把每行的矩形,计算最大面积,就可以得到结果,代码如下:

class Solution {public:     int largestRectangleArea(vector<int> &height) {         stack<int> st;         int s = height.size(), ans = 0, h, w;         for (int i = 0; i < s; i++) {             while (!st.empty() && height[st.top()] >= height[i]) {                 h = height[st.top()];                 st.pop();                 if (st.empty())                     w = i;                 else                    w = i - 1 - st.top();                 if (ans < h * w) ans = h * w;             }             st.push(i);         }                while (!st.empty()) {             h = height[st.top()];             st.pop();             w = st.empty() ? s: s - 1 - st.top();             if (ans < h * w) ans = h * w;         }         return ans;     }    int maximalRectangle(vector<vector<char>>& matrix) {        int rows = matrix.size();        if (rows == 0)            return 0;        int cols = matrix[0].size(), count;        vector<vector<int>>vec(rows, vector<int>(cols, 0));        for (int i = 0; i < rows; i++) {            for (int j = 0; j < cols; j++) {                if (i == 0 && matrix[i][j] == '1')                    vec[i][j] = 1;                else if (matrix[i][j] == '1') {                    vec[i][j] = vec[i - 1][j] + 1;                }            }        }        int res = 0;        for (int i = 0; i < rows; i++) {            int temp = largestRectangleArea(vec[i]);            if (temp > res)                res = temp;        }        return res;    }};

然后丢下84的解法链接LeetCode 84

原创粉丝点击