{"id":229,"date":"2024-01-10T20:13:37","date_gmt":"2024-01-10T12:13:37","guid":{"rendered":"https:\/\/wangqianming.top\/?p=229"},"modified":"2024-01-10T20:13:37","modified_gmt":"2024-01-10T12:13:37","slug":"%e6%a0%884-%e5%8f%8c%e6%8c%87%e9%92%881","status":"publish","type":"post","link":"https:\/\/wangqianming.top\/index.php\/2024\/01\/10\/%e6%a0%884-%e5%8f%8c%e6%8c%87%e9%92%881\/","title":{"rendered":"\u68084 \u53cc\u6307\u94881"},"content":{"rendered":"<h1>\u4e00\u9053leetcode\u56f0\u96be\u9898\u7684\u4e24\u79cd\u89e3\u6cd5<\/h1>\n<h2>\u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62<\/h2>\n<p>\u7ed9\u5b9a n \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<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<p>\u8fd9\u9053\u9898\u7684\u5173\u952e\u5728\u4e8e\u627e\u5230\u6bcf\u4e2a\u67f1\u5b50\u5de6\u8fb9\u548c\u53f3\u8fb9\u7b2c\u4e00\u4e2a\u5c0f\u4e8e\u5b83\u7684\u9ad8\u5ea6\u7684\u67f1\u5b50\uff0c\u8fd9\u6837\u7684\u8bdd\u53ef\u4ee5\u4f7f\u7528\u5355\u8c03\u6808\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">class Solution {\n    public int largestRectangleArea(int[] heights) {\n        int n = heights.length;\n        int[] left = new int[n];\n        int[] right = new int[n];\n\n        Deque&lt;Integer&gt; mono_stack = new ArrayDeque&lt;Integer&gt;();\n        for (int i = 0; i &lt; n; ++i) {\n            while (!mono_stack.isEmpty() &amp;&amp; heights[mono_stack.peek()] &gt;= heights[i]) {\n                mono_stack.pop();\n            }\n            left[i] = (mono_stack.isEmpty() ? -1 : mono_stack.peek());\n            mono_stack.push(i);\n        }\n\n        mono_stack.clear();\n        for (int i = n - 1; i &gt;= 0; --i) {\n            while (!mono_stack.isEmpty() &amp;&amp; heights[mono_stack.peek()] &gt;= heights[i]) {\n                mono_stack.pop();\n            }\n            right[i] = (mono_stack.isEmpty() ? n : mono_stack.peek());\n            mono_stack.push(i);\n        }\n\n        int ans = 0;\n        for (int i = 0; i &lt; n; ++i) {\n            ans = Math.max(ans, (right[i] - left[i] - 1) * heights[i]);\n        }\n        return ans;\n    }\n}<\/code><\/pre>\n<p>\u4f46\u662f\u5355\u8c03\u6808\u6ca1\u6709\u4e0b\u9762\u53cc\u6307\u9488\u7684\u65b9\u6cd5\u5feb\uff1a<\/p>\n<pre><code class=\"language-java\">class Solution {\n    public int largestRectangleArea(int[] heights) {\n        \/\/ \u53cc\u6307\u9488\uff1a\u5bf9\u6bcf\u4e2aheight[i]\u627e\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u548c\u53f3\u8fb9\u7b2c\u4e00\u4e2a\u5c0f\u4e8e\u5b83\u7684\u67f1\u5b50\u4e0b\u6807\uff0c\u4e24\u4e2a\u4e0b\u8868\u4e4b\u95f4\u5c31\u662fheight[i]\u4e3a\u9ad8\u7684\u7684\u6700\u5927\u77e9\u5f62\u7684\u5bbd\n        int len = heights.length;\n        int[] minLeft = new int[len];\n        int[] minRight = new int[len];\n        minLeft[0] = -1;\n        for (int i = 1; i &lt; len; i++) {\n            int j = i - 1;\n            while (j &gt;= 0 &amp;&amp; heights[j] &gt;= heights[i])\n                j = minLeft[j];\/\/ \u53cc\u6307\u9488\u7684\u4f18\u5316\u4e4b\u5904\u5728\u4e8e\u6b64\uff0c\u8bb0\u5fc6\u5316\u7684\u601d\u60f3\u8bb0\u5f55\u5de6\u8fb9\u7684minLeft\u503c\u907f\u514d\u91cd\u590d\u641c\u7d22\n            minLeft[i] = j;\n        }\n        minRight[len - 1] = len;\n        for (int i = len - 2; i &gt;= 0; i--) {\n            int j = i + 1;\n            while (j &lt; len &amp;&amp; heights[j] &gt;= heights[i])\n                j = minRight[j];\/\/ \u53cc\u6307\u9488\u7684\u4f18\u5316\u4e4b\u5904\n            minRight[i] = j;\n        }\n        int sum = 0;\n        for (int i = 0; i &lt; len; i++) {\n            sum = Math.max(sum, heights[i] * (minRight[i] - minLeft[i] - 1));\n        }\n        return sum;\n\n    }\n}<\/code><\/pre>\n<p>\u53cc\u6307\u9488\u548c\u5355\u8c03\u6808\u7684\u65b9\u6cd5\u90fd\u5229\u7528\u5355\u8c03\u6027\u6765\u4f7f\u7528\u5df2\u7ecf\u6c42\u51fa\u7684\u7ed3\u679c\u7b80\u5316\u6c42\u503c\u8fc7\u7a0b\uff0c\u4f46\u6570\u7ec4\u7d22\u5f15\u6bd5\u7adf\u8fd8\u662f\u6bd4\u5165\u6808\u51fa\u6808\u6765\u7684\u5feb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u9053leetcode\u56f0\u96be\u9898\u7684\u4e24\u79cd\u89e3\u6cd5 \u67f1\u72b6\u56fe\u4e2d\u6700\u5927\u7684\u77e9\u5f62 \u7ed9\u5b9a n \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 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,23,21,9],"class_list":["post-229","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java","tag-23","tag-21","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":1,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/229\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/229\/revisions\/230"}],"wp:attachment":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/media?parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/categories?post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/tags?post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}