Convert 1D Array Into 2D Array (LeetCode 2022): Solution and Explanation

Easy
Array
Matrix
Simulation

Given an array original and a target shape m x n, lay the values out row by row into an m x n grid. If the array has more or fewer than m * n values, return an empty 2D array.

Intuition

Think of original as a paper tape. An m x n grid read row by row is just that tape cut into m pieces of length n and stacked.

The only thing that can go wrong is the length. If the tape is shorter than m * n, some grid cells stay empty; if it is longer, values are left over. Both cases have no valid answer.

Once the length matches, row i is simply the slice from index i * n up to (i + 1) * n. No searching or rearranging is involved.

Approach: Size check, then slice every n values

  1. If m * n != len(original), return [].
  2. Create an empty result list.
  3. For each i from 0 to m - 1, append original[i * n:(i + 1) * n].
  4. Return the result.

Step-by-step example

original = [1,2,3,4], m = 2, n = 2
  1. m * n = 4, which equals the length of original, so a 2 x 2 grid is possible.
  2. i = 0: cut indexes 0..1, giving row [1,2].
  3. i = 1: cut indexes 2..3, giving row [3,4].
  4. The result is [[1,2],[3,4]].

Code: Python, Java, C++

1class Solution:
2    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:
3        if m * n != len(original):
4            return []
5        res = []
6        for i in range(m):
7            res.append(original[i * n:(i + 1) * n])
8        return res
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(m * n)

Every value is copied into the grid exactly once; the size check is O(1).

Space

O(m * n)

The returned grid holds all m * n values. Nothing else grows with the input.

Other approaches

Index arithmetic per value (time O(m * n), space O(m * n))

Allocate an m x n grid and walk k over original, writing original[k] to grid[k // n][k % n]. Same cost, useful when the language has no cheap slice.

Two cursors (time O(m * n), space O(m * n))

Keep a row and a column cursor, advance the column for every value and wrap to the next row when it reaches n. Equivalent work with more bookkeeping.

Common mistakes

  • Skipping the size check: slicing past the end silently produces short rows in Python and JavaScript instead of failing.
  • Allocating the m x n grid before checking the size. With m and n up to 40,000 that is 1.6 billion cells for an input that should return [].
  • Using n and m the wrong way round: each row has n values and there are m rows.
  • In C++ or Java, m * n fits in a 32-bit int under these constraints, but casting to a wider type is a cheap guard if the limits change.

Frequently asked questions

When does the answer have to be empty?

Exactly when m * n is different from the length of original. Every value must be used and every cell must be filled, so the two counts have to match.

Why is row i the slice from i * n to (i + 1) * n?

Rows hold n values each, so rows 0 to i - 1 use up the first i * n values. Row i starts right after them and takes the next n.

What is the time and space complexity?

O(m * n) time to copy every value once, and O(m * n) space for the returned grid.

How is this different from Reshape the Matrix?

Reshape the Matrix starts from a 2D grid and returns the original grid on a size mismatch. Here the input is already flat, so each row is a plain slice, and a mismatch returns an empty array.