來源:力扣(LeetCode)
連接:https://leetcode-cn.com/problems/maximal-square
著做權歸領釦網絡全部。商業轉載請聯繫官方受權,非商業轉載請註明出處。javascript
在一個由 0 和 1 組成的二維矩陣內,找到只包含 1 的最大正方形,並返回其面積。java
示例:數組
輸入:網絡
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0code
輸出: 4blog
/** * @param {character[][]} matrix * @return {number} */ var maximalSquare = function(matrix) { let m = matrix.length; if(m == 0) return 0; let n = matrix[0].length; let dp = new Array(m+1).fill(0).map(() => new Array(n+1).fill(0)); let max = 0; for(let i=0; i<m; i++){ for(let j=0; j<n; j++){ if(i ==0 || j==0){ dp[i][j] = parseInt(matrix[i][j]); max = Math.max(max, dp[i][j]); continue; }else if(matrix[i][j] == '1'){ dp[i][j] = Math.min(dp[i-1][j],dp[i-1][j-1],dp[i][j-1]) + 1; max = Math.max(max, dp[i][j]); }else{ dp[i][j] = 0; } } } return max * max; };