<?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>diagonal Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/diagonal/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/diagonal/</link>
	<description></description>
	<lastBuildDate>Sat, 05 Sep 2020 17:45:16 +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>diagonal Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/diagonal/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1572. Matrix Diagonal Sum</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1572-matrix-diagonal-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1572-matrix-diagonal-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Sep 2020 17:44:45 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[diagonal]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7327</guid>

					<description><![CDATA[<p>Given a&#160;square&#160;matrix&#160;mat, return the sum of the matrix diagonals. Only include the sum of all the elements on the primary diagonal and all the elements&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1572-matrix-diagonal-sum/">花花酱 LeetCode 1572. Matrix Diagonal Sum</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>Given a&nbsp;square&nbsp;matrix&nbsp;<code>mat</code>, return the sum of the matrix diagonals.</p>



<p>Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[<strong>1</strong>,2,<strong>3</strong>],
&nbsp;             [4,<strong>5</strong>,6],
&nbsp;             [<strong>7</strong>,8,<strong>9</strong>]]
<strong>Output:</strong> 25
<strong>Explanation: </strong>Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Notice that element mat[1][1] = 5 is counted only once.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[<strong>1</strong>,1,1,<strong>1</strong>],
&nbsp;             [1,<strong>1</strong>,<strong>1</strong>,1],
&nbsp;             [1,<strong>1</strong>,<strong>1</strong>,1],
&nbsp;             [<strong>1</strong>,1,1,<strong>1</strong>]]
<strong>Output:</strong> 8
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[<strong>5</strong>]]
<strong>Output:</strong> 5
</pre>



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



<ul><li><code>n == mat.length == mat[i].length</code></li><li><code>1 &lt;= n &lt;= 100</code></li><li><code>1 &lt;= mat[i][j] &lt;= 100</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Note: if n is odd, be careful not to double count the center one.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int diagonalSum(vector&lt;vector&lt;int&gt;&gt;&amp; mat) {
    const int n = mat.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      ans += mat[i][i] + mat[i][n - i - 1];
    if (n &amp; 1) ans -= mat[n / 2][n / 2];
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1572-matrix-diagonal-sum/">花花酱 LeetCode 1572. Matrix Diagonal Sum</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/algorithms/array/leetcode-1572-matrix-diagonal-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1424. Diagonal Traverse II</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1424-diagonal-traverse-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1424-diagonal-traverse-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Apr 2020 07:37:05 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[diagonal]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6665</guid>

					<description><![CDATA[<p>Given a list of lists of integers,&#160;nums,&#160;return all elements of&#160;nums&#160;in diagonal order as shown in the below images. Example 1: Input: nums = [[1,2,3],[4,5,6],[7,8,9]] Output:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1424-diagonal-traverse-ii/">花花酱 LeetCode 1424. Diagonal Traverse II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1424. Diagonal Traverse II - 刷题找工作 EP322" width="500" height="375" src="https://www.youtube.com/embed/9J3IRkjkIkQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a list of lists of integers,&nbsp;<code>nums</code>,&nbsp;return all elements of&nbsp;<code>nums</code>&nbsp;in diagonal order as shown in the below images.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [1,4,2,7,5,3,8,6,9]
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
<strong>Output:</strong> [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
<strong>Output:</strong> [1,4,2,5,3,8,6,9,7,10,11]
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [[1,2,3,4,5,6]]
<strong>Output:</strong> [1,2,3,4,5,6]
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10^5</code></li><li><code>1 &lt;= nums[i].length &lt;=&nbsp;10^5</code></li><li><code>1 &lt;= nums[i][j] &lt;= 10^9</code></li><li>There at most&nbsp;<code>10^5</code>&nbsp;elements in&nbsp;<code>nums</code>.</li></ul>



<h2><strong>Solution: Hashtable</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/04/1424-ep322.png" alt="" class="wp-image-6676" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/04/1424-ep322.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/04/1424-ep322-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/04/1424-ep322-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Use diagonal index (i + j) as key.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; findDiagonalOrder(vector&lt;vector&lt;int&gt;&gt;&amp; nums) {
    vector&lt;vector&lt;int&gt;&gt; m;    
    for (int i = 0; i &lt; nums.size(); ++i)
      for (int j = 0; j &lt; nums[i].size(); ++j) {
        if (m.size() &lt;= i + j) m.push_back({});
        m[i + j].push_back(nums[i][j]);
    }
    vector&lt;int&gt; ans;    
    for (const auto&amp; d : m)
      ans.insert(end(ans), rbegin(d), rend(d));
    return ans;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def findDiagonalOrder(self, nums):
    m = []
    for i, row in enumerate(nums):
      for j, v in enumerate(row):
        if i + j &gt;= len(m): m.append([])
        m[i+j].append(v)
    return [v for d in m for v in reversed(d)]</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1424-diagonal-traverse-ii/">花花酱 LeetCode 1424. Diagonal Traverse II</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/hashtable/leetcode-1424-diagonal-traverse-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1329. Sort the Matrix Diagonally</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1329-sort-the-matrix-diagonally/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1329-sort-the-matrix-diagonally/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Jan 2020 16:58:39 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[diagonal]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6130</guid>

					<description><![CDATA[<p>Given a&#160;m * n&#160;matrix&#160;mat&#160;of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array. Example 1: Input:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1329-sort-the-matrix-diagonally/">花花酱 LeetCode 1329. Sort the Matrix Diagonally</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>Given a&nbsp;<code>m * n</code>&nbsp;matrix&nbsp;<code>mat</code>&nbsp;of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
<strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
</pre>



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



<ul><li><code>m ==&nbsp;mat.length</code></li><li><code>n ==&nbsp;mat[i].length</code></li><li><code>1 &lt;= m, n&nbsp;&lt;= 100</code></li><li><code>1 &lt;= mat[i][j] &lt;= 100</code></li></ul>



<h2><strong>Solution: HashTable</strong></h2>



<p>Collect each diagonal&#8217;s (keyed by i &#8211; j) elements into an array and sort it separately.<br>If  we offset the key by n, e.g. i &#8211; j + n, we can use an array instead of a hashtable.</p>



<p>Time complexity: O(m*n + (m+n) * (m+n) * log(m + n))) =  (n^2*logn)<br>Space complexity: O(m*n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; diagonalSort(vector&lt;vector&lt;int&gt;&gt;&amp; mat) {
    const int m = mat.size();
    const int n = mat[0].size();
    vector&lt;deque&lt;int&gt;&gt; qs(m + n);
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        qs[i - j + n].push_back(mat[i][j]);
    for (auto&amp; q : qs)
      sort(begin(q), end(q));
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j) {
        mat[i][j] = qs[i - j + n].front();
        qs[i - j + n].pop_front();
      }
    return mat;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1329-sort-the-matrix-diagonally/">花花酱 LeetCode 1329. Sort the Matrix Diagonally</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/hashtable/leetcode-1329-sort-the-matrix-diagonally/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
