Advertisement
HXXXXJ

221. Maximal Square

Apr 14th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.71 KB | None | 0 0
  1.  func maximalSquare(_ matrix: [[Character]]) -> Int {
  2.         guard matrix.count > 0 && matrix[0].count > 0 else { return 0}
  3.         let n = matrix.count
  4.         let m = matrix[0].count
  5.         var dp = [[Int]](repeating: [Int](repeating: 0, count: m), count: n)
  6.        
  7.         var count = 0
  8.         for i in  0 ..< n{
  9.             for j in 0 ..< m {
  10.                 if matrix[i][j] == "1"{
  11.                     dp[i][j] = 1
  12.                     if i > 0 && j > 0 {
  13.                         dp[i][j] = min(dp[i - 1][j] , dp[i - 1][j - 1], dp[i][j - 1]) + 1
  14.                     }
  15.                     count = max(count ,  dp[i][j] )
  16.                 }
  17.             }
  18.         }
  19.         return count * count
  20.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement