{"id":2402,"date":"2022-01-26T14:12:44","date_gmt":"2022-01-26T06:12:44","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2402"},"modified":"2022-01-26T14:12:45","modified_gmt":"2022-01-26T06:12:45","slug":"leetcode84","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/26\/leetcode84\/","title":{"rendered":"leetcode84 &#8211;\u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62"},"content":{"rendered":"\n<p>\u7ed9\u5b9a&nbsp;<em>n<\/em>&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u7528\u6765\u8868\u793a\u67f1\u72b6\u56fe\u4e2d\u5404\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\u3002\u6bcf\u4e2a\u67f1\u5b50\u5f7c\u6b64\u76f8\u90bb\uff0c\u4e14\u5bbd\u5ea6\u4e3a 1 \u3002<\/p>\n\n\n\n<p>\u6c42\u5728\u8be5\u67f1\u72b6\u56fe\u4e2d\uff0c\u80fd\u591f\u52fe\u52d2\u51fa\u6765\u7684\u77e9\u5f62\u7684\u6700\u5927\u9762\u79ef\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/04\/histogram.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>heights = &#91;2,1,5,6,2,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>10\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u5927\u7684\u77e9\u5f62\u4e3a\u56fe\u4e2d\u7ea2\u8272\u533a\u57df\uff0c\u9762\u79ef\u4e3a 10<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/04\/histogram-1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong> heights = &#91;2,4]\n<strong>\u8f93\u51fa\uff1a<\/strong> 4<\/code><\/pre>\n\n\n\n<p>\u66b4\u529b\u6c42\u89e3\uff1a\u4f1a\u8d85\u65f6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># &#91;84] \u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62\n#\n\n# @lc code=start\nclass Solution:\n    def largestRectangleArea(self, heights: List&#91;int]) -&gt; int:\n        maxs=0\n        for i in range(len(heights)):\n            for j in range(i,len(heights)):\n                ms=min(heights&#91;i:j+1])\n                maxs=max(maxs,ms*(j-i+1))\n        return maxs\n# @lc code=end<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u6808\u6c42\u89e3\uff1a<\/p>\n\n\n\n<ul><li>\u9996\u5148\u6211\u4eec\u679a\u4e3e\u67d0\u4e00\u6839\u67f1\u5b50\u00a0<em>i<\/em>\u00a0\u4f5c\u4e3a\u9ad8\u00a0<em>h<\/em>=<em>heights<\/em>[<em>i<\/em>]\uff1b<\/li><li>\u968f\u540e\u6211\u4eec\u9700\u8981\u8fdb\u884c\u5411\u5de6\u53f3\u4e24\u8fb9\u6269\u5c55\uff0c\u4f7f\u5f97\u6269\u5c55\u5230\u7684\u67f1\u5b50\u7684\u9ad8\u5ea6\u5747\u4e0d\u5c0f\u4e8e\u00a0<em>h<\/em>\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230<strong>\u5de6\u53f3\u4e24\u4fa7\u6700\u8fd1\u7684\u9ad8\u5ea6\u5c0f\u4e8e\u00a0<em>h<\/em>\u00a0\u7684\u67f1\u5b50<\/strong>\uff0c\u8fd9\u6837\u8fd9\u4e24\u6839\u67f1\u5b50\u4e4b\u95f4\uff08\u4e0d\u5305\u62ec\u5176\u672c\u8eab\uff09\u7684\u6240\u6709\u67f1\u5b50\u9ad8\u5ea6\u5747\u4e0d\u5c0f\u4e8e\u00a0<em>h<\/em>\uff0c\u5e76\u4e14\u5c31\u662f\u00a0<em>i<\/em>\u00a0\u80fd\u591f\u6269\u5c55\u5230\u7684\u6700\u8fdc\u8303\u56f4\u3002<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u7528\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u00a0[6, 7, 5, 2, 4, 5, 9, 3]\u00a0\u6765\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u5355\u8c03\u6808\u3002\u6211\u4eec\u9700\u8981\u6c42\u51fa\u6bcf\u4e00\u6839\u67f1\u5b50\u7684\u5de6\u4fa7\u4e14\u6700\u8fd1\u7684\u5c0f\u4e8e\u5176\u9ad8\u5ea6\u7684\u67f1\u5b50\u3002\u521d\u59cb\u65f6\u7684\u6808\u4e3a\u7a7a\u3002<\/p>\n\n\n\n<ul><li>\u6211\u4eec\u679a\u4e3e\u00a06\uff0c\u56e0\u4e3a\u6808\u4e3a\u7a7a\uff0c\u6240\u4ee5\u00a06\u00a0\u5de6\u4fa7\u7684\u67f1\u5b50\u662f\u300c\u54e8\u5175\u300d\uff0c\u4f4d\u7f6e\u4e3a -1\u3002\u968f\u540e\u6211\u4eec\u5c06\u00a06\u00a0\u5165\u6808\u3002<ul><li>\u6808\uff1a<code>[6(0)]<\/code>\u3002\uff08\u8fd9\u91cc\u62ec\u53f7\u5185\u7684\u6570\u5b57\u8868\u793a\u67f1\u5b50\u5728\u539f\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\uff09<\/li><\/ul><\/li><li>\u6211\u4eec\u679a\u4e3e\u00a07\uff0c\u7531\u4e8e\u00a06&lt;7\uff0c\u56e0\u6b64\u4e0d\u4f1a\u79fb\u9664\u6808\u9876\u5143\u7d20\uff0c\u6240\u4ee5\u00a07\u00a0\u5de6\u4fa7\u7684\u67f1\u5b50\u662f\u00a06\uff0c\u4f4d\u7f6e\u4e3a\u00a00\u3002\u968f\u540e\u6211\u4eec\u5c06\u00a07\u00a0\u5165\u6808\u3002<ul><li>\u6808\uff1a<code>[6(0), 7(1)]<\/code><\/li><\/ul><\/li><li>\u6211\u4eec\u679a\u4e3e\u00a05\uff0c\u7531\u4e8e\u00a07\u22655\uff0c\u56e0\u6b64\u79fb\u9664\u6808\u9876\u5143\u7d20\u00a07\u3002\u540c\u6837\u5730\uff0c6\u22655\uff0c\u518d\u79fb\u9664\u6808\u9876\u5143\u7d20\u00a06\u3002\u6b64\u65f6\u6808\u4e3a\u7a7a\uff0c\u6240\u4ee5\u00a05\u00a0\u5de6\u4fa7\u7684\u67f1\u5b50\u662f\u300c\u54e8\u5175\u300d\uff0c\u4f4d\u7f6e\u4e3a\u00a0\u22121\u3002\u968f\u540e\u6211\u4eec\u5c06\u00a05\u00a0\u5165\u6808\u3002<ul><li>\u6808\uff1a<code>[5(2)]<\/code><\/li><\/ul><\/li><li>\u63a5\u4e0b\u6765\u7684\u679a\u4e3e\u8fc7\u7a0b\u4e5f\u5927\u540c\u5c0f\u5f02\u3002\u6211\u4eec\u679a\u4e3e\u00a02\uff0c\u79fb\u9664\u6808\u9876\u5143\u7d20\u00a05\uff0c\u5f97\u5230\u00a02\u00a0\u5de6\u4fa7\u7684\u67f1\u5b50\u662f\u300c\u54e8\u5175\u300d\uff0c\u4f4d\u7f6e\u4e3a\u00a0\u22121\u3002\u5c06\u00a02\u00a0\u5165\u6808\u3002<ul><li>\u6808\uff1a<code>[2(3)]<\/code><\/li><\/ul><\/li><li>\u6211\u4eec\u679a\u4e3e4\uff0c5\u00a0\u548c\u00a09\uff0c\u90fd\u4e0d\u4f1a\u79fb\u9664\u4efb\u4f55\u6808\u9876\u5143\u7d20\uff0c\u5f97\u5230\u5b83\u4eec\u5de6\u4fa7\u7684\u67f1\u5b50\u5206\u522b\u662f\u00a02\uff0c4\u00a0\u548c\u00a05\uff0c\u4f4d\u7f6e\u5206\u522b\u4e3a\u00a03\uff0c4\u00a0\u548c\u00a05\u3002\u5c06\u5b83\u4eec\u5165\u6808\u3002<ul><li>\u6808\uff1a<code>[2(3), 4(4), 5(5), 9(6)]<\/code><\/li><\/ul><\/li><li>\u6211\u4eec\u679a\u4e3e\u00a03\uff0c\u4f9d\u6b21\u79fb\u9664\u6808\u9876\u5143\u7d20\u00a09\uff0c5\u00a0\u548c\u00a04\uff0c\u5f97\u5230\u00a03\u00a0\u5de6\u4fa7\u7684\u67f1\u5b50\u662f\u00a02\uff0c\u4f4d\u7f6e\u4e3a\u00a03\u3002\u5c06\u00a03\u00a0\u5165\u6808\u3002<ul><li>\u6808\uff1a<code>[2(3), 3(7)]<\/code><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>\u8fd9\u6837\u4ee5\u6765\uff0c\u6211\u4eec\u5f97\u5230\u5b83\u4eec\u5de6\u4fa7\u7684\u67f1\u5b50\u7f16\u53f7\u5206\u522b\u4e3a\u00a0[-1, 0, -1, -1, 3, 4, 5, 3]\u3002\u7528\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u4ece\u53f3\u5411\u5de6\u8fdb\u884c\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u5f97\u5230\u5b83\u4eec\u53f3\u4fa7\u7684\u67f1\u5b50\u7f16\u53f7\u5206\u522b\u4e3a\u00a0[2, 2, 3, 8, 7, 7, 7, 8]\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u4f4d\u7f6e\u00a08\u00a0\u770b\u4f5c\u300c\u54e8\u5175\u300d\u3002<\/p>\n\n\n\n<p>\u5728\u5f97\u5230\u4e86\u5de6\u53f3\u4e24\u4fa7\u7684\u67f1\u5b50\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6bcf\u6839\u67f1\u5b50\u5bf9\u5e94\u7684\u5de6\u53f3\u8fb9\u754c\uff0c\u5e76\u6c42\u51fa\u7b54\u6848\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\r\n# @lc code=start\r\nclass Solution:\r\n    def largestRectangleArea(self, heights: List&#91;int]) -> int:\r\n        # maxs=0\r\n        # for i in range(len(heights)):\r\n        #     for j in range(i,len(heights)):\r\n        #         ms=min(heights&#91;i:j+1])\r\n        #         maxs=max(maxs,ms*(j-i+1))\r\n        # return maxs\r\n        n = len(heights)\r\n        left, right = &#91;0] * n, &#91;0] * n\r\n\r\n        mono_stack = list()\r\n        for i in range(n):\r\n            while mono_stack and heights&#91;mono_stack&#91;-1]] >= heights&#91;i]:\r\n                mono_stack.pop()\r\n            left&#91;i] = mono_stack&#91;-1] if mono_stack else -1\r\n            mono_stack.append(i)\r\n        \r\n        mono_stack = list()\r\n        for i in range(n - 1, -1, -1):\r\n            while mono_stack and heights&#91;mono_stack&#91;-1]] >= heights&#91;i]:\r\n                mono_stack.pop()\r\n            right&#91;i] = mono_stack&#91;-1] if mono_stack else n\r\n            mono_stack.append(i)\r\n        \r\n        ans = max((right&#91;i] - left&#91;i] - 1) * heights&#91;i] for i in range(n)) if n > 0 else 0\r\n        return ans<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a&nbsp;n&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u7528\u6765\u8868\u793a\u67f1\u72b6\u56fe\u4e2d\u5404\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\u3002\u6bcf\u4e2a\u67f1\u5b50\u5f7c\u6b64\u76f8\u90bb\uff0c\u4e14\u5bbd\u5ea6\u4e3a 1 \u3002  &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/26\/leetcode84\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcode84 &#8211;\u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2402"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2402"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2402\/revisions"}],"predecessor-version":[{"id":2407,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2402\/revisions\/2407"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2402"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}