<?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>update Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/update/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/update/</link>
	<description></description>
	<lastBuildDate>Sun, 14 Jun 2020 03:42:56 +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>update Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/update/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1476. Subrectangle Queries</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 14 Jun 2020 03:41:08 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[update]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6909</guid>

					<description><![CDATA[<p>Implement the class&#160;SubrectangleQueries&#160;which receives a&#160;rows x cols&#160;rectangle as a matrix of integers in the constructor and supports two methods: 1.&#160;updateSubrectangle(int row1, int col1, int row2,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/">花花酱 LeetCode 1476. Subrectangle Queries</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>Implement the class&nbsp;<code>SubrectangleQueries</code>&nbsp;which receives a&nbsp;<code>rows x cols</code>&nbsp;rectangle as a matrix of integers in the constructor and supports two methods:</p>



<p>1.<code>&nbsp;updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)</code></p>



<ul><li>Updates all values with&nbsp;<code>newValue</code>&nbsp;in the subrectangle whose upper left coordinate is&nbsp;<code>(row1,col1)</code>&nbsp;and bottom right coordinate is&nbsp;<code>(row2,col2)</code>.</li></ul>



<p>2.<code>&nbsp;getValue(int row, int col)</code></p>



<ul><li>Returns the current value of the coordinate&nbsp;<code>(row,col)</code>&nbsp;from&nbsp;the rectangle.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]
[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
<strong>Output</strong>
</pre>


<p>[null,1,null,5,5,null,10,5]</p>



<p><strong>Explanation</strong>
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);  
// The initial rectangle (4&#215;3) looks like:
// 1 2 1
// 4 3 4
// 3 2 1
// 1 1 1
subrectangleQueries.getValue(0, 2); // return 1
subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
// After this update the rectangle looks like:
// 5 5 5
// 5 5 5
// 5 5 5
// 5 5 5 
subrectangleQueries.getValue(0, 2); // return 5
subrectangleQueries.getValue(3, 1); // return 5
subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
// After this update the rectangle looks like:
// 5   5   5
// 5   5   5
// 5   5   5
// 10  10  10 
subrectangleQueries.getValue(3, 1); // return 10
subrectangleQueries.getValue(0, 2); // return 5
</p>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]
[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
<strong>Output</strong>
</pre>


<p>[null,1,null,100,100,null,20]</p>



<p><strong>Explanation</strong>
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
subrectangleQueries.getValue(0, 0); // return 1
subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
subrectangleQueries.getValue(0, 0); // return 100
subrectangleQueries.getValue(2, 2); // return 100
subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
subrectangleQueries.getValue(2, 2); // return 20
</p>



<p><strong>Constraints:</strong></p>



<ul><li>There will be at most&nbsp;<code>500</code>&nbsp;operations considering both methods:&nbsp;<code>updateSubrectangle</code>&nbsp;and&nbsp;<code>getValue</code>.</li><li><code>1 &lt;= rows, cols &lt;= 100</code></li><li><code>rows ==&nbsp;rectangle.length</code></li><li><code>cols == rectangle[i].length</code></li><li><code>0 &lt;= row1 &lt;= row2 &lt; rows</code></li><li><code>0 &lt;= col1 &lt;= col2 &lt; cols</code></li><li><code>1 &lt;= newValue, rectangle[i][j] &lt;= 10^9</code></li><li><code>0 &lt;= row &lt; rows</code></li><li><code>0 &lt;= col &lt; cols</code></li></ul>



<h2><strong>Solution 1: Simulation</strong></h2>



<p>Update the matrix values.</p>



<p>Time complexity: <br>Update: O(m*n), where m*n is the area of the sub-rectangle.<br>Query: O(1)</p>



<p>Space complexity: O(rows*cols) </p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class SubrectangleQueries {
public:
  SubrectangleQueries(vector&lt;vector&lt;int&gt;&gt;&amp; rectangle): 
    m_(rectangle) {}

  void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
    for (int i = row1; i &lt;= row2; ++i)
      for (int j = col1; j &lt;= col2; ++j)
        m_[i][j] = newValue;
  }

  int getValue(int row, int col) {
    return m_[row][col];
  }
private:
  vector&lt;vector&lt;int&gt;&gt; m_;
};</pre>
</div></div>



<h2><strong>Solution 2: Geometry</strong></h2>



<p>For each update remember the region and value.</p>



<p>For each query, find the newest updates that covers the query point. If not found, return the original value in the matrix.</p>



<p>Time complexity:<br>Update: O(1)<br>Query: O(|U|), where |U| is the number of updates so far.</p>



<p>Space complexity: O(|U|)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class SubrectangleQueries {
public:
  SubrectangleQueries(vector&lt;vector&lt;int&gt;&gt;&amp; rectangle): 
    m_(rectangle) {}

  void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
    updates_.push_front({row1, col1, row2, col2, newValue});
  }

  int getValue(int row, int col) {
    for (const auto&amp; u : updates_)
      if (row &gt;= u[0] &amp;&amp; row &lt;= u[2] &amp;&amp; col &gt;= u[1] &amp;&amp; col &lt;= u[3])
        return u[4];
    return m_[row][col];
  }
private:
  const vector&lt;vector&lt;int&gt;&gt;&amp; m_;
  deque&lt;vector&lt;int&gt;&gt; updates_;  
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1476-subrectangle-queries/">花花酱 LeetCode 1476. Subrectangle Queries</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/geometry/leetcode-1476-subrectangle-queries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
