{"id":1526,"date":"2018-01-07T13:17:28","date_gmt":"2018-01-07T21:17:28","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1526"},"modified":"2018-09-03T10:25:14","modified_gmt":"2018-09-03T17:25:14","slug":"leetcode-755-pour-water","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-755-pour-water\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 755. Pour Water"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 755. Pour Water - \u5237\u9898\u627e\u5de5\u4f5c EP151\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/sgDdhNTByLQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a<\/p>\n<p>\u7ed9\u4f60\u5730\u5f62\u7684\u9ad8\u5ea6\uff0c\u6709V\u5355\u4f4d\u7684\u6c34\u4f1a\u4eceK\u4f4d\u7f6e\u843d\u4e0b\uff0c\u95ee\u4f60\u7a33\u5b9a\u4e4b\u540e\u6c34\u4f4d\u7684\u9ad8\u5ea6\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>We are given an elevation map,\u00a0<code>heights[i]<\/code>\u00a0representing the height of the terrain at that index. The width at each index is 1. After\u00a0<code>V<\/code>\u00a0units of water fall at index\u00a0<code>K<\/code>, how much water is at each index?<\/p>\n<p>Water first drops at index\u00a0<code>K<\/code>\u00a0and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>If the droplet would eventually fall by moving left, then move left.<\/li>\n<li>Otherwise, if the droplet would eventually fall by moving right, then move right.<\/li>\n<li>Otherwise, rise at it&#8217;s current position.<\/li>\n<\/ul>\n<p>Here, &#8220;eventually fall&#8221; means that the droplet will eventually be at a lower level if it moves in that direction. Also, &#8220;level&#8221; means the height of the terrain plus any water in that column.<\/p>\n<p>&nbsp;<\/p>\n<p>We can assume there&#8217;s infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block &#8211; each unit of water has to be in exactly one block.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1683\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1684\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep151-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><br \/>\n<\/ins><\/p>\n<h1><strong>Solution 1: Simulation<\/strong><\/h1>\n<p>Time complexity: O(V*n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; pourWater(vector&lt;int&gt;&amp; heights, int V, int K) {\r\n        while (V--) drop(heights, K);\r\n        return heights;    \r\n    }\r\nprivate:\r\n    void drop(vector&lt;int&gt;&amp; heights, int K) {\r\n        int best = K;\r\n        for (int d = -1; d &lt;= 1; d += 2) {\r\n            int i = K + d;\r\n            while (i &gt;= 0 &amp;&amp; i &lt; heights.size() \r\n                   &amp;&amp; heights[i] &lt;= heights[i - d]) {\r\n                if (heights[i] &lt; heights[best]) best = i;\r\n                i += d;\r\n            }\r\n            if (best != K) break;\r\n        }\r\n        ++heights[best];\r\n    }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\n  public int[] pourWater(int[] heights, int V, int K) {\r\n    while (V-- &gt; 0) {\r\n      dropWater(heights, K);\r\n    }\r\n    return heights;\r\n  }\r\n  \r\n  private void dropWater(int[] heights, int K) {\r\n    int best = K;\r\n    for (int d = -1; d &lt;= 1; d += 2) {      \r\n      int i = K + d;\r\n      while (i &gt;= 0 &amp;&amp; i &lt; heights.length &amp;&amp; heights[i] &lt;= heights[i - d]) {\r\n        if (heights[i] &lt; heights[best]) best = i;\r\n        i += d;\r\n      }      \r\n      if (best != K) break;\r\n    }\r\n    ++heights[best];\r\n  } \r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 207 ms\r\n\"\"\"\r\nclass Solution:\r\n  def pourWater(self, heights, V, K):\r\n    def drop(h, K):\r\n      best = K\r\n      for d in (-1, 1):\r\n        i = K + d\r\n        while i &gt;= 0 and i &lt; len(h) and h[i] &lt;= h[i - d]:\r\n          if h[i] &lt; h[best]:\r\n            best = i\r\n          i += d\r\n        if best != K:\r\n          break\r\n      heights[best] += 1\r\n\r\n    for _ in range(V):\r\n      drop(heights, K)\r\n    return heights\r\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a \u7ed9\u4f60\u5730\u5f62\u7684\u9ad8\u5ea6\uff0c\u6709V\u5355\u4f4d\u7684\u6c34\u4f1a\u4eceK\u4f4d\u7f6e\u843d\u4e0b\uff0c\u95ee\u4f60\u7a33\u5b9a\u4e4b\u540e\u6c34\u4f4d\u7684\u9ad8\u5ea6\u3002 Problem: We are given an elevation map,\u00a0heights[i]\u00a0representing the height of the terrain at that index. The width at each index is 1. After\u00a0V\u00a0units&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,179,207],"class_list":["post-1526","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-simulation","tag-water","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1526"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1526\/revisions"}],"predecessor-version":[{"id":3830,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1526\/revisions\/3830"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}