|
| 1 | + |
| 2 | +## 题目地址 |
| 3 | + |
| 4 | +https://leetcode.com/problems/number-of-islands/ |
| 5 | + |
| 6 | +## 题目描述 |
| 7 | + |
| 8 | + |
| 9 | +``` |
| 10 | +Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +
|
| 14 | +Input: |
| 15 | +11110 |
| 16 | +11010 |
| 17 | +11000 |
| 18 | +00000 |
| 19 | +
|
| 20 | +Output: 1 |
| 21 | +Example 2: |
| 22 | +
|
| 23 | +Input: |
| 24 | +11000 |
| 25 | +11000 |
| 26 | +00100 |
| 27 | +00011 |
| 28 | +
|
| 29 | +Output: 3 |
| 30 | +``` |
| 31 | + |
| 32 | +## 思路 |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +符合直觉的做法是暴力求解处所有的正方形,逐一计算面积,然后记录最大的。这种时间复杂度很好。 |
| 37 | + |
| 38 | +我们考虑使用动态规划,我们使用dp[i][j]表示以matrix[i][j]为右下角的顶点的可以组成的最大正方形的边长。 |
| 39 | +那么我们只需要计算所有的i,j组合,然后求出最大值即可。 |
| 40 | + |
| 41 | +我们来看下dp[i][j] 怎么推导。 首先我们要看matrix[i][j], 如果matrix[i][j]等于0,那么就不用看了,直接等于0。 |
| 42 | +如果matrix[i][j]等于1,那么我们将matrix[[i][j]分别往上和往左进行延伸,直到碰到一个0为止。 |
| 43 | + |
| 44 | +如图 dp[3][3] 的计算。 matrix[3][3]等于1,我们分别往上和往左进行延伸,直到碰到一个0为止,上面长度为1,左边为3。 |
| 45 | +dp[2][2]等于1(之前已经计算好了),那么其实这里的瓶颈在于三者的最小值, 即`Min(1, 1, 3)`, 也就是`1`。 那么dp[3][3] 就等于 |
| 46 | +`Min(1, 1, 3) + 1`。 |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +dp[i - 1][j - 1]我们直接拿到,关键是`往上和往左进行延伸`, 最直观的做法是我们内层加一个循环去做就好了。 |
| 51 | +但是我们仔细观察一下,其实我们根本不需要这样算。 我们可以直接用dp[i - 1][j]和dp[i][j -1]。 |
| 52 | +具体就是`Min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1`。 |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +事实上,这道题还有空间复杂度O(N)的解法,其中N指的是列数。 |
| 57 | +大家可以去这个[leetcode讨论](https://leetcode.com/problems/maximal-square/discuss/61803/C%2B%2B-space-optimized-DP)看一下。 |
| 58 | +## 关键点解析 |
| 59 | + |
| 60 | +- DP |
| 61 | +- 递归公式可以利用dp[i - 1][j]和dp[i][j -1]的计算结果,而不用重新计算 |
| 62 | +- 空间复杂度可以降低到O(n), n为列数 |
| 63 | + |
| 64 | +## 代码 |
| 65 | + |
| 66 | +```js |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +/* |
| 71 | + * @lc app=leetcode id=221 lang=javascript |
| 72 | + * |
| 73 | + * [221] Maximal Square |
| 74 | + */ |
| 75 | +/** |
| 76 | + * @param {character[][]} matrix |
| 77 | + * @return {number} |
| 78 | + */ |
| 79 | +var maximalSquare = function(matrix) { |
| 80 | + if (matrix.length === 0) return 0; |
| 81 | + const dp = []; |
| 82 | + const rows = matrix.length; |
| 83 | + const cols = matrix[0].length; |
| 84 | + let max = Number.MIN_VALUE; |
| 85 | + |
| 86 | + for (let i = 0; i < rows + 1; i++) { |
| 87 | + if (i === 0) { |
| 88 | + dp[i] = Array(cols + 1).fill(0); |
| 89 | + } else { |
| 90 | + dp[i] = [0]; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + for (let i = 1; i < rows + 1; i++) { |
| 95 | + for (let j = 1; j < cols + 1; j++) { |
| 96 | + if (matrix[i - 1][j - 1] === "1") { |
| 97 | + dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1; |
| 98 | + max = Math.max(max, dp[i][j]); |
| 99 | + } else { |
| 100 | + dp[i][j] = 0; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return max * max; |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | + |
0 commit comments