<?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>row major Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/row-major/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/row-major/</link>
	<description></description>
	<lastBuildDate>Sun, 24 Oct 2021 03:35:49 +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>row major Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/row-major/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2022. Convert 1D Array Into 2D Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2022-convert-1d-array-into-2d-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2022-convert-1d-array-into-2d-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Oct 2021 03:34:07 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[row major]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8633</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;1-dimensional (1D) integer array&#160;original, and two integers,&#160;m&#160;and&#160;n. You are tasked with creating a 2-dimensional (2D) array with&#160;m&#160;rows and&#160;n&#160;columns using&#160;all&#160;the elements from&#160;original. The&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2022-convert-1d-array-into-2d-array/">花花酱 LeetCode 2022. Convert 1D Array Into 2D Array</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;1-dimensional (1D) integer array&nbsp;<code>original</code>, and two integers,&nbsp;<code>m</code>&nbsp;and&nbsp;<code>n</code>. You are tasked with creating a 2-dimensional (2D) array with&nbsp;<code>m</code>&nbsp;rows and&nbsp;<code>n</code>&nbsp;columns using&nbsp;<strong>all</strong>&nbsp;the elements from&nbsp;<code>original</code>.</p>



<p>The elements from indices&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n - 1</code>&nbsp;(<strong>inclusive</strong>) of&nbsp;<code>original</code>&nbsp;should form the first row of the constructed 2D array, the elements from indices&nbsp;<code>n</code>&nbsp;to&nbsp;<code>2 * n - 1</code>&nbsp;(<strong>inclusive</strong>) should form the second row of the constructed 2D array, and so on.</p>



<p>Return&nbsp;<em>an&nbsp;</em><code>m x n</code><em>&nbsp;2D array constructed according to the above procedure, or an empty 2D array if it is impossible</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/08/26/image-20210826114243-1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> original = [1,2,3,4], m = 2, n = 2
<strong>Output:</strong> [[1,2],[3,4]]
<strong>Explanation:
</strong>The constructed 2D array should contain 2 rows and 2 columns.
The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> original = [1,2,3], m = 1, n = 3
<strong>Output:</strong> [[1,2,3]]
<strong>Explanation:</strong>
The constructed 2D array should contain 1 row and 3 columns.
Put all three elements in original into the first row of the constructed 2D array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> original = [1,2], m = 1, n = 1
<strong>Output:</strong> []
<strong>Explanation:
</strong>There are 2 elements in original.
It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> original = [3], m = 1, n = 2
<strong>Output:</strong> []
<strong>Explanation:</strong>
There is 1 element in original.
It is impossible to make 1 element fill all the spots in a 1x2 2D array, so return an empty 2D array.
</pre>



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



<ul><li><code>1 &lt;= original.length &lt;= 5 * 10<sup>4</sup></code></li><li><code>1 &lt;= original[i] &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= m, n &lt;= 4 * 10<sup>4</sup></code></li></ul>



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



<p>the i-th element in original array will have index (i//n, i % n) in the 2D array.</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; construct2DArray(vector&lt;int&gt;&amp; original, int m, int n) {
    if (original.size() != m * n) return {};
    vector&lt;vector&lt;int&gt;&gt; ans(m, vector&lt;int&gt;(n));
    for (int i = 0; i &lt; m * n; ++i)
      ans[i / n][i % n] = original[i];
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2022-convert-1d-array-into-2d-array/">花花酱 LeetCode 2022. Convert 1D Array Into 2D Array</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-2022-convert-1d-array-into-2d-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Why grid[y][x] instead of grid[x][y]?</title>
		<link>https://zxi.mytechroad.com/blog/cs/why-gridyx-instead-of-gridxy/</link>
					<comments>https://zxi.mytechroad.com/blog/cs/why-gridyx-instead-of-gridxy/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 18 Jul 2020 00:59:50 +0000</pubDate>
				<category><![CDATA[CS]]></category>
		<category><![CDATA[column major]]></category>
		<category><![CDATA[cs]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[row major]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7111</guid>

					<description><![CDATA[<p>If you don&#8217;t like grid[y][x], you can use grid[r][c] instead. [crayon-663965ff7a26e812559408/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/cs/why-gridyx-instead-of-gridxy/">Why grid[y][x] instead of grid[x][y]?</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-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Why grid[y][x] instead of grid[x][y]? - CS大讲堂 EP16" width="500" height="281" src="https://www.youtube.com/embed/P50r4CDeUYw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



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



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



<p>If you don&#8217;t like grid[y][x], you can use grid[r][c] instead.</p>



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

<pre class="crayon-plain-tag">#include &lt;iostream&gt;

using namespace std;

int main(int argc, char** argv) {
  constexpr int kRows = 2;
  constexpr int kCols = 3;
  int arr[kRows][kCols] = {{1,2,3},{4,5,6}};
  for (int r = 0; r &lt; kRows; ++r) {
    for (int c = 0; c &lt; kCols; ++c)
      cout &lt;&lt; arr[r][c] &lt;&lt; &quot; &quot;;
    cout &lt;&lt; endl;
  }

  for (int r = 0; r &lt; kRows; ++r)
    for (int c = 0; c &lt; kCols; ++c)
      cout &lt;&lt; &amp;(arr[r][c]) - &amp;(arr[0][0]) &lt;&lt; &quot; &quot;;
  return 0;
}

// Output:
// 1 2 3
// 4 5 6
// 0 1 2 3 4 5</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/cs/why-gridyx-instead-of-gridxy/">Why grid[y][x] instead of grid[x][y]?</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/cs/why-gridyx-instead-of-gridxy/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
