LeetCode 221. Maximal Square (javascript solution)

Description:

Given an m x n binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.

Solution:

Time Complexity : O(n^2)
Space Complexity: O(n^2)

var maximalSquare = function(matri…


This content originally appeared on DEV Community and was authored by codingpineapple

Description:

Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Solution:

Time Complexity : O(n^2)
Space Complexity: O(n^2)

var maximalSquare = function(matrix) {
        const rows = matrix.length, cols = rows > 0 ? matrix[0].length : 0;
        // Create dp array
        const dp = Array(rows + 1).fill(0).map(() => Array(cols + 1).fill(0));

        // Keep trac of the max square length
        let maxsqlen = 0;
        for (let i = 1; i <= rows; i++) {
            for (let j = 1; j <= cols; j++) {
                // Only check cells that have a 1 in the original array
                if (matrix[i-1][j-1] == '1'){
                    // Check if the current cell is part of a square
                    dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
                    maxsqlen = Math.max(maxsqlen, dp[i][j]);
                }
            }
        }
        // Return the area of the square
        return maxsqlen * maxsqlen;
};


This content originally appeared on DEV Community and was authored by codingpineapple


Print Share Comment Cite Upload Translate Updates
APA

codingpineapple | Sciencx (2021-04-25T01:35:32+00:00) LeetCode 221. Maximal Square (javascript solution). Retrieved from https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/

MLA
" » LeetCode 221. Maximal Square (javascript solution)." codingpineapple | Sciencx - Sunday April 25, 2021, https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/
HARVARD
codingpineapple | Sciencx Sunday April 25, 2021 » LeetCode 221. Maximal Square (javascript solution)., viewed ,<https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/>
VANCOUVER
codingpineapple | Sciencx - » LeetCode 221. Maximal Square (javascript solution). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/
CHICAGO
" » LeetCode 221. Maximal Square (javascript solution)." codingpineapple | Sciencx - Accessed . https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/
IEEE
" » LeetCode 221. Maximal Square (javascript solution)." codingpineapple | Sciencx [Online]. Available: https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/. [Accessed: ]
rf:citation
» LeetCode 221. Maximal Square (javascript solution) | codingpineapple | Sciencx | https://www.scien.cx/2021/04/25/leetcode-221-maximal-squarejavascript-solution/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.