{"id":521,"date":"2020-09-26T10:51:39","date_gmt":"2020-09-26T02:51:39","guid":{"rendered":"http:\/\/39.96.58.60\/?p=521"},"modified":"2022-10-18T16:38:55","modified_gmt":"2022-10-18T08:38:55","slug":"leetcode-221-%e6%9c%80%e5%a4%a7%e6%ad%a3%e6%96%b9%e5%bd%a2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=521","title":{"rendered":"Leetcode 221. \u6700\u5927\u6b63\u65b9\u5f62"},"content":{"rendered":"<p>\u5728\u4e00\u4e2a\u7531 0 \u548c 1 \u7ec4\u6210\u7684\u4e8c\u7ef4\u77e9\u9635\u5185\uff0c\u627e\u5230\u53ea\u5305\u542b 1 \u7684\u6700\u5927\u6b63\u65b9\u5f62\uff0c\u5e76\u8fd4\u56de\u5176\u9762\u79ef\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><strong>\u8f93\u5165: \n\n<\/strong>\n\n1 0 1 0 0\n\n1 0 <strong>1 1<\/strong> 1\n\n1 1 <strong>1 1 <\/strong>1\n\n1 0 0 1 0\n\n\n\n<strong>\u8f93\u51fa: <\/strong>4<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u52a8\u6001\u89c4\u5212\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a84 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8693.21% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8627.86% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u60f3\uff1a\n\n\"\"\"\nclass Solution:\n    def maximalSquare(self, matrix: List[List[str]]) -&gt; int:\n        if matrix == []:\n            return 0\n        m, n = len(matrix), len(matrix[0])\n        max_ = 0\n        dp = [[0 for _ in range(n)] for _ in range(m)]\n        for i in range(m):\n            dp[i][0] = int(matrix[i][0])\n            max_ = max(dp[i][0], max_)\n\n        for j in range(1, n):\n            dp[0][j] = int(matrix[0][j])\n            max_ = max(dp[0][j], max_)\n\n        for i in range(1, m):\n            for j in range(1, n):\n                if matrix[i][j] == '1':\n                    dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1\n                else:\n                    dp[i][j] = 0\n\n                max_ = max(dp[i][j], max_)\n        return max_**2\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a120 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8630.10% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.4 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8661.68% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6574\u7406\u4ee3\u7801\u540e\uff0c\u6548\u7387\u53d8\u4f4e\uff0c\u4f46\u662f\u903b\u8f91\u4e0a\u66f4\u987a\u7545\n\"\"\"\nclass Solution:\n    def maximalSquare(self, matrix: List[List[str]]) -&gt; int:\n        if matrix == []:\n            return 0\n        m, n = len(matrix), len(matrix[0])\n        max_ = 0\n        dp = [[0 for _ in range(n+1)] for _ in range(m+1)]\n\n        for i in range(m):\n            for j in range(n):\n                if matrix[i][j] == '1':\n                    dp[i+1][j+1] = min(dp[i+1-1][j+1-1], dp[i+1][j+1-1], dp[i+1-1][j+1]) + 1\n                else:\n                    dp[i+1][j+1] = 0\n\n                max_ = max(dp[i+1][j+1], max_)\n        return max_**2<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e00\u4e2a\u7531 0 \u548c 1 \u7ec4\u6210\u7684\u4e8c\u7ef4\u77e9\u9635\u5185\uff0c\u627e\u5230\u53ea\u5305\u542b 1 \u7684\u6700\u5927\u6b63\u65b9\u5f62\uff0c\u5e76\u8fd4\u56de\u5176\u9762\u79ef\u3002 \u793a\u4f8b: \u8f93\u5165: 1 0 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/521"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=521"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":522,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions\/522"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}