{"id":2148,"date":"2022-01-19T18:17:05","date_gmt":"2022-01-19T10:17:05","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2148"},"modified":"2022-01-19T18:17:06","modified_gmt":"2022-01-19T10:17:06","slug":"leetcodeday42","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/19\/leetcodeday42\/","title":{"rendered":"leetcodeday42 &#8211;\u63a5\u96e8\u6c34"},"content":{"rendered":"\n<p>\u7ed9\u5b9a&nbsp;<code>n<\/code>&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\u8868\u793a\u6bcf\u4e2a\u5bbd\u5ea6\u4e3a&nbsp;<code>1<\/code>&nbsp;\u7684\u67f1\u5b50\u7684\u9ad8\u5ea6\u56fe\uff0c\u8ba1\u7b97\u6309\u6b64\u6392\u5217\u7684\u67f1\u5b50\uff0c\u4e0b\u96e8\u4e4b\u540e\u80fd\u63a5\u591a\u5c11\u96e8\u6c34\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-241.png\" alt=\"\" class=\"wp-image-2149\" width=\"430\" height=\"171\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-241.png 792w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-241-300x120.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-241-768x306.png 768w\" sizes=\"(max-width: 430px) 100vw, 430px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]\n<strong>\u8f93\u51fa\uff1a<\/strong>6\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e0a\u9762\u662f\u7531\u6570\u7ec4 [0,1,0,2,1,0,1,3,2,1,2,1] \u8868\u793a\u7684\u9ad8\u5ea6\u56fe\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u63a5 6 \u4e2a\u5355\u4f4d\u7684\u96e8\u6c34\uff08\u84dd\u8272\u90e8\u5206\u8868\u793a\u96e8\u6c34\uff09\u3002 \n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>height = [4,2,0,3,2,5]\n<strong>\u8f93\u51fa\uff1a<\/strong>9\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>n == height.length<\/code><\/li><li><code>1 &lt;= n &lt;= 2 * 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= height[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>\u521d\u6b21\u5c1d\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\r\n# @lc app=leetcode.cn id=42 lang=python3\r\n#\r\n# &#91;42] \u63a5\u96e8\u6c34\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def trap(self, height: List&#91;int]) -> int:\r\n        lens=len(height)\r\n        i=0\r\n        rain=0\r\n        all=0\r\n        no=0\r\n        start=1\r\n        while i&lt;lens-1:\r\n           # print(i,start)\r\n            if start>=lens:\r\n                height&#91;i]=max(height&#91;i+1:]) if i+1&lt;lens else height&#91;i]\r\n                # i=i+1\r\n                start=i+1\r\n                continue\r\n\r\n            if height&#91;i]>height&#91;start]:\r\n                start=start+1\r\n            elif height&#91;i]&lt;=height&#91;start]:\r\n                all=all+(start-i-1)*height&#91;i]\r\n                for j in range(i+1,start):\r\n                    no= no + height&#91;j]\r\n                    \r\n                    rain = rain+(all-no)\r\n                    all=no=0\r\n                i=start\r\n                start=i+1\r\n            \r\n\r\n        return rain\r\n            <\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-242.png\" alt=\"\" class=\"wp-image-2150\" width=\"332\" height=\"132\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-242.png 692w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-242-300x119.png 300w\" sizes=\"(max-width: 332px) 100vw, 332px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a&nbsp;n&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\u8868\u793a\u6bcf\u4e2a\u5bbd\u5ea6\u4e3a&nbsp;1&nbsp;\u7684\u67f1\u5b50\u7684\u9ad8\u5ea6\u56fe\uff0c\u8ba1\u7b97\u6309\u6b64\u6392\u5217\u7684 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/19\/leetcodeday42\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday42 &#8211;\u63a5\u96e8\u6c34<\/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\/2148"}],"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=2148"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2148\/revisions"}],"predecessor-version":[{"id":2151,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2148\/revisions\/2151"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2148"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}