{ "question_id": 85, "question_title": "最大矩形", "difficulty": "困难", "question_content": "<p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg\" style=\"width: 402px; height: 322px;\" /><pre><strong>输入:</strong>matrix = [[\"1\",\"0\",\"1\",\"0\",\"0\"],[\"1\",\"0\",\"1\",\"1\",\"1\"],[\"1\",\"1\",\"1\",\"1\",\"1\"],[\"1\",\"0\",\"0\",\"1\",\"0\"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[\"0\"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[\"1\"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [[\"0\",\"0\"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>rows == matrix.length</code></li>\t<li><code>cols == matrix[0].length</code></li>\t<li><code>0 <= row, cols <= 200</code></li>\t<li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul>", "topic_link": "https://bbs.csdn.net/topics/600470828", "cpp": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic inline int max(int a, int b)\n{\n\treturn a > b ? a : b;\n}\nstatic int area_calc(int *heights, int size)\n{\n\tint *indexes = malloc(size * sizeof(int));\n\tint *lhist = malloc(size * sizeof(int));\n\tint *rhist = malloc(size * sizeof(int));\n\tint i, pos = 0;\n\tfor (i = 0; i < size; i++)\n\t{\n\t\twhile (pos > 0 && heights[indexes[pos - 1]] >= heights[i])\n\t\t{\n\t\t\tpos--;\n\t\t}\n\t\tlhist[i] = pos == 0 ? -1 : indexes[pos - 1];\n\t\tindexes[pos++] = i;\n\t}\n\tpos = 0;\n\tfor (i = size - 1; i >= 0; i--)\n\t{\n\t\twhile (pos > 0 && heights[indexes[pos - 1]] >= heights[i])\n\t\t{\n\t\t\tpos--;\n\t\t}\n\t\trhist[i] = pos == 0 ? size : indexes[pos - 1];\n\t\tindexes[pos++] = i;\n\t}\n\tint max_area = 0;\n\tfor (i = 0; i < size; i++)\n\t{\n\t\tint area = heights[i] * (rhist[i] - lhist[i] - 1);\n\t\tmax_area = max(area, max_area);\n\t}\n\treturn max_area;\n}\nstatic int maximalRectangle(char **matrix, int matrixRowSize, int matrixColSize)\n{\n\tint i, j, max_area = 0;\n\tint *heights = malloc(matrixColSize * sizeof(int));\n\tmemset(heights, 0, matrixColSize * sizeof(int));\n\tfor (i = 0; i < matrixRowSize; i++)\n\t{\n\t\tfor (j = 0; j < matrixColSize; j++)\n\t\t{\n\t\t\theights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;\n\t\t}\n\t\tmax_area = max(max_area, area_calc(heights, matrixColSize));\n\t}\n\treturn max_area;\n}\nint main(int argc, char **argv)\n{\n\tif (argc < 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test row1 row2...\\n\");\n\t\texit(-1);\n\t}\n\tint i, j;\n\tint row_size = argc - 1;\n\tint col_size = strlen(argv[1]);\n\tfor (i = 0; i < row_size; i++)\n\t{\n\t\tprintf(\"%s\\n\", argv[i + 1]);\n\t}\n\tprintf(\"%d\\n\", maximalRectangle(argv + 1, argc - 1, strlen(argv[1])));\n\treturn 0;\n}", "java": "class Solution {\n\tpublic int maximalRectangle(char[][] matrix) {\n\t\tif (matrix == null || matrix.length == 0)\n\t\t\treturn 0;\n\t\tint m = matrix.length;\n\t\tint n = matrix[0].length;\n\t\tint[] left = new int[n];\n\t\tint[] right = new int[n];\n\t\tint[] height = new int[n];\n\t\tArrays.fill(right, n);\n\t\tint cur_left = 0;\n\t\tint cur_right = n;\n\t\tint res = 0;\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tcur_left = 0;\n\t\t\tcur_right = n;\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (matrix[i][j] == '1')\n\t\t\t\t\theight[j]++;\n\t\t\t\telse\n\t\t\t\t\theight[j] = 0;\n\t\t\t}\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (matrix[i][j] == '1') {\n\t\t\t\t\tleft[j] = Math.max(left[j], cur_left);\n\t\t\t\t} else {\n\t\t\t\t\tleft[j] = 0;\n\t\t\t\t\tcur_left = j + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = n - 1; j >= 0; j--) {\n\t\t\t\tif (matrix[i][j] == '1') {\n\t\t\t\t\tright[j] = Math.min(right[j], cur_right);\n\t\t\t\t} else {\n\t\t\t\t\tright[j] = n;\n\t\t\t\t\tcur_right = j;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t\tres = Math.max(res, (right[j] - left[j]) * height[j]);\n\t\t}\n\t\treturn res;\n\t}\n}\n", "js": "", "python": "class Solution(object):\n\tdef maximalRectangle(self, matrix):\n\t\t\"\"\"\n\t\t:type matrix: List[List[str]]\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tif matrix is None or len(matrix) == 0:\n\t\t\treturn 0\n\t\tls_row, ls_col = len(matrix), len(matrix[0])\n\t\tleft, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col\n\t\tmaxA = 0\n\t\tfor i in range(ls_row):\n\t\t\tcurr_left, curr_right = 0, ls_col\n\t\t\tfor j in range(ls_col):\n\t\t\t\tif matrix[i][j] == '1':\n\t\t\t\t\theight[j] += 1\n\t\t\t\telse:\n\t\t\t\t\theight[j] = 0\n\t\t\tfor j in range(ls_col):\n\t\t\t\tif matrix[i][j] == '1':\n\t\t\t\t\tleft[j] = max(left[j], curr_left)\n\t\t\t\telse:\n\t\t\t\t\tleft[j], curr_left = 0, j + 1\n\t\t\tfor j in range(ls_col - 1, -1, -1):\n\t\t\t\tif matrix[i][j] == '1':\n\t\t\t\t\tright[j] = min(right[j], curr_right)\n\t\t\t\telse:\n\t\t\t\t\tright[j], curr_right = ls_col, j\n\t\t\tfor j in range(ls_col):\n\t\t\t\tmaxA = max(maxA, (right[j] - left[j]) * height[j])\n\t\treturn maxA\n# %%\ns = Solution()\nmatrix = [[\"1\",\"0\",\"1\",\"0\",\"0\"],[\"1\",\"0\",\"1\",\"1\",\"1\"],[\"1\",\"1\",\"1\",\"1\",\"1\"],[\"1\",\"0\",\"0\",\"1\",\"0\"]]\nprint(s.maximalRectangle(matrix))", "status": 1, "keywords": "栈,数组,动态规划,矩阵,单调栈", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://github.com/zhangyu345293721/leetcode" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/84/84_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/84/84_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/84/84_java.ipynb?type=file" }, "notebook_enable": 1 }