<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>water Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/water/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/water/</link>
	<description></description>
	<lastBuildDate>Mon, 03 Sep 2018 17:25:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>water Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/water/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 755. Pour Water</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-755-pour-water/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-755-pour-water/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 07 Jan 2018 21:17:28 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[water]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1526</guid>

					<description><![CDATA[<p>题目大意： 给你地形的高度，有V单位的水会从K位置落下，问你稳定之后水位的高度。 Problem: We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each index is 1. After V units&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-755-pour-water/">花花酱 LeetCode 755. Pour Water</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/sgDdhNTByLQ?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：</p>
<p>给你地形的高度，有V单位的水会从K位置落下，问你稳定之后水位的高度。</p>
<p><strong>Problem:</strong></p>
<p>We are given an elevation map, <code>heights[i]</code> representing the height of the terrain at that index. The width at each index is 1. After <code>V</code> units of water fall at index <code>K</code>, how much water is at each index?</p>
<p>Water first drops at index <code>K</code> and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:</p>
<p>&nbsp;</p>
<ul>
<li>If the droplet would eventually fall by moving left, then move left.</li>
<li>Otherwise, if the droplet would eventually fall by moving right, then move right.</li>
<li>Otherwise, rise at it&#8217;s current position.</li>
</ul>
<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>
<p>&nbsp;</p>
<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>
<p><strong>Idea:</strong></p>
<p><img 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="(max-width: 960px) 100vw, 960px" /> <img 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="(max-width: 960px) 100vw, 960px" /><br />
<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 />
</ins></p>
<h1><strong>Solution 1: Simulation</strong></h1>
<p>Time complexity: O(V*n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
    vector&lt;int&gt; pourWater(vector&lt;int&gt;&amp; heights, int V, int K) {
        while (V--) drop(heights, K);
        return heights;    
    }
private:
    void drop(vector&lt;int&gt;&amp; heights, int K) {
        int best = K;
        for (int d = -1; d &lt;= 1; d += 2) {
            int i = K + d;
            while (i &gt;= 0 &amp;&amp; i &lt; heights.size() 
                   &amp;&amp; heights[i] &lt;= heights[i - d]) {
                if (heights[i] &lt; heights[best]) best = i;
                i += d;
            }
            if (best != K) break;
        }
        ++heights[best];
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
  public int[] pourWater(int[] heights, int V, int K) {
    while (V-- &gt; 0) {
      dropWater(heights, K);
    }
    return heights;
  }
  
  private void dropWater(int[] heights, int K) {
    int best = K;
    for (int d = -1; d &lt;= 1; d += 2) {      
      int i = K + d;
      while (i &gt;= 0 &amp;&amp; i &lt; heights.length &amp;&amp; heights[i] &lt;= heights[i - d]) {
        if (heights[i] &lt; heights[best]) best = i;
        i += d;
      }      
      if (best != K) break;
    }
    ++heights[best];
  } 
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 207 ms
"""
class Solution:
  def pourWater(self, heights, V, K):
    def drop(h, K):
      best = K
      for d in (-1, 1):
        i = K + d
        while i &gt;= 0 and i &lt; len(h) and h[i] &lt;= h[i - d]:
          if h[i] &lt; h[best]:
            best = i
          i += d
        if best != K:
          break
      heights[best] += 1

    for _ in range(V):
      drop(heights, K)
    return heights</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-755-pour-water/">花花酱 LeetCode 755. Pour Water</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-755-pour-water/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
