{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "1dfcbc2e42034d68a9cb9bca18c32f96",
  "keywords": "栈,数组,动态规划,矩阵,单调栈",
  "title": "最大矩形",
  "desc": [
    {
      "content": "\n<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>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#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}",
    "language": "cpp"
  },
  "node_id": "dailycode-569fed9589644294ae5ef549b7babf31",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470828"
}