bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 221. Maximal Square

221. Maximal Square

Leetcode Solutions

Dynamic Programming Approach

  1. Initialize maxSquareLength to 0 to keep track of the maximum length of a square found so far.
  2. Create a 2D DP array dp with the same dimensions as the input matrix, initialized to 0.
  3. Iterate over each cell (i, j) in the input matrix. a. If matrix[i][j] is 1, update dp[i][j] to min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1. b. Update maxSquareLength to the maximum of its current value and dp[i][j].
  4. After completing the iteration, return the area of the largest square, which is maxSquareLength * maxSquareLength.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...