436. Maximal Square

436. Maximal Square

Tag:

Dynamic Problem.

Main Idea:

2D Matrix Rolling Array Problem. Let   d p [ i ] [ j ] \ dp[i][j] means the maximum square edge. The first thing to do is initialization, for all   d p [ 0 ] [ j ] = m a t r i x [ 0 ] [ j ] \ dp[0][j] = matrix[0][j] , and   d p [ i ] [ 0 ] = m a t r i x [ i ] [ 0 ] \ dp[i][0] = matrix[i][0] . The update equation is IF   m a t r i x [ i ] [ j ] \ matrix[i][j] is true, then   d p [ i ] [ j ] = 1 + m i n ( d p [ i 1 ] [ j 1 ] , d p [ i ] [ j 1 ] , d p [ i 1 ] [ j ] ) \ dp[i][j] = 1 + min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) .

For optimization, we can reserve only two row for lookup, which would be row[i] and row[i-1].

Tips/Notes:

  1. In c++, min/max function can only compare two elements.
  2.   m a t r i x [ i ] [ j ] \ matrix[i][j] and   d p [ i ] [ j ] \ dp[i][j] can be confusing sometimes. Note that the update equation uses   m a t r i x [ i ] [ j ] \ matrix[i][j] as condition, but update   d p [ i ] [ j ] \ dp[i][j] .
  3. Finally, iterate the matrix, find the maximum   d p [ i ] [ j ] \ dp[i][j] and return the square of it.

Time/Space Cost:

Time Cost:   O ( n m ) \ O(nm)
Space Cost:   O ( n m ) \ O(nm) ,   O ( 2 n ) \ O(2n) for optimization.

Code:

class Solution {
public:
    /**
     * @param matrix: a matrix of 0 and 1
     * @return: an integer
     */
    int maxSquare(vector<vector<int>> &matrix) {
        // write your code here]
        int row = matrix.size();
        int col = (row == 0)? 0 : matrix[0].size();
        
        vector<vector<int>> dp(row, vector<int>(col,0) );
        
        // initialization
        for(int i = 0; i < row; i++){
            dp[i][0] = matrix[i][0];
        }
        
        for(int j = 0; j < col; j++){
            dp[0][j] = matrix[0][j];
        }
        
        // update dp[i][j]
        for(int i = 1; i < row; i++){
            for(int j = 1; j < col; j++){
                if(matrix[i][j])
                	// note1 mentioned above
                    dp[i][j] = 1 + min(min(dp[i-1][j-1], dp[i-1][j]), dp[i][j-1]);
                
            }
        }
        
        int ans = 0;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                ans = max(ans, dp[i][j]);
            }
        }
        
        return ans * ans;
    }
};

Followup Problem:

  1. what if we want to get the maximum diagonal matrix, which the numbers on the diagonal are 1 only.

Main Idea:

Based on the original problem, the update equation would be:   d p [ i ] [ j ] = 1 + m i n ( d p [ i 1 ] [ j 1 ] , L e f t [ i ] [ j 1 ] , U p [ i 1 ] [ j ] ) \ dp[i][j] = 1 + min(dp[i-1][j-1], Left[i][j-1], Up[i-1][j]) . Apparently, we need to maintain three matrix   d p [ i ] [ j ] , L e f t [ i ] [ j ] , U p [ i ] [ j ] \ dp[i][j], Left[i][j], Up[i][j] .

Time/Space Cost:

Time Cost:   O ( n m ) \ O(nm)
Space Cost:   O ( 3 n m ) \ O(3nm) ,   O ( 2 n 3 ) \ O(2n * 3) for optimization.

发布了10 篇原创文章 · 获赞 0 · 访问量 103

猜你喜欢

转载自blog.csdn.net/Zahb44856/article/details/103901155
今日推荐