{"id":8633,"date":"2021-10-23T20:34:07","date_gmt":"2021-10-24T03:34:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8633"},"modified":"2021-10-23T20:35:49","modified_gmt":"2021-10-24T03:35:49","slug":"leetcode-2022-convert-1d-array-into-2d-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2022-convert-1d-array-into-2d-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2022. Convert 1D Array Into 2D Array"},"content":{"rendered":"\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/08\/26\/image-20210826114243-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> original = [1,2,3,4], m = 2, n = 2\n<strong>Output:<\/strong> [[1,2],[3,4]]\n<strong>Explanation:\n<\/strong>The constructed 2D array should contain 2 rows and 2 columns.\nThe first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.\nThe second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> original = [1,2,3], m = 1, n = 3\n<strong>Output:<\/strong> [[1,2,3]]\n<strong>Explanation:<\/strong>\nThe constructed 2D array should contain 1 row and 3 columns.\nPut all three elements in original into the first row of the constructed 2D array.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> original = [1,2], m = 1, n = 1\n<strong>Output:<\/strong> []\n<strong>Explanation:\n<\/strong>There are 2 elements in original.\nIt is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> original = [3], m = 1, n = 2\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong>\nThere is 1 element in original.\nIt is impossible to make 1 element fill all the spots in a 1x2 2D array, so return an empty 2D array.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>the i-th element in original array will have index (i\/\/n, i % n) in the 2D array.<\/p>\n\n\n\n<p>Time complexity: O(n*m)<br>Space complexity: O(n*m)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n    if (original.size() != m * n) return {};\n    vector<vector<int>> ans(m, vector<int>(n));\n    for (int i = 0; i < m * n; ++i)\n      ans[i \/ n][i % n] = original[i];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;1-dimensional (1D) integer array&nbsp;original, and two integers,&nbsp;m&nbsp;and&nbsp;n. You are tasked with creating a 2-dimensional (2D) array with&nbsp;m&nbsp;rows and&nbsp;n&nbsp;columns using&nbsp;all&nbsp;the elements from&nbsp;original. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,216,632],"class_list":["post-8633","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-matrix","tag-row-major","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8633","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=8633"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8633\/revisions"}],"predecessor-version":[{"id":8636,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8633\/revisions\/8636"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}