码迷,mamicode.com
首页 > 其他好文 > 详细

221. Maximal Square

时间:2017-07-22 00:06:35      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:msu   ble   amp   red   class   type   blank   itil   length   

https://leetcode.com/problems/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:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 4.

 

 

Sol 1:

 

Brute Force.

 

class Solution(object):
    def maximalSquare(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        # brute force

        if len(matrix) <= 0 :
            return 0
        
        
        rows = len(matrix) 
        cols = len(matrix[0])
        
        maxsqlen = 0
        for i in range(rows):
            for j in range(cols):
                # initilize flag to be True
                if matrix[i][j] == 1:
                    sqlen = 1
                    flag = True
                    # border limits 
                    while sqlen + i < rows and sqlen + j < cols and flag:
                        # advance square length downwords 
                        for k in range(j, sqlen + j + 1):
                            if matrix[i+sqlen][k] == 0:
                                flag = False
                                break
                        # advance square length to the right
                        for k in range(i, sqlen + i + 1):
                            if matrix[k][j+sqlen] == 0:
                                flag = False
                                break
                                
                        if flag:
                            sqlen += 1
                    
                    if maxsqlen < sqlen:
                        maxsqlen = sqlen
                        
        return maxsqlen * maxsqlen 

 

Complexity Analysis

  • Time complexity : O\big((mn)^2\big)O((mn)?2??). In worst case, we need to traverse the complete matrix for every 1.
  • Space complexity : O(1)O(1). No extra space is used.

 

 

Sol 2:

 

DP.

 

(sth. wrong with initlization of DP...)

 

class Solution(object):
    def maximalSquare(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        # DP
        # Time complexity : O(mn)O(mn). Single pass.
        # pace complexity : O(mn)O(mn). Another matrix of same size is used for dp.

        if len(matrix) <= 0 :
            return 0
        
        
        rows = len(matrix) 
        cols = len(matrix[0])
        
        maxsqlen = 0
        dp = [0] * (rows + 1) * (cols + 1)
        
        for i in range(1, rows+1):
            for j in range(1, cols+1):
                if matrix[i-1][j-1] == 1:
                    dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1
                    maxsqlen = max(maxsqlen, dp[i][j])
                    
        return maxsqlen * maxsqlen
        

 

221. Maximal Square

标签:msu   ble   amp   red   class   type   blank   itil   length   

原文地址:http://www.cnblogs.com/prmlab/p/7220060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!