Convert 1D Array Into 2D Array

Easy Topics Company Tags

Arrange every value in original into a matrix with m rows and n columns, preserving its order. Fill the first row from left to right, then the second row, and continue in this order. Return the matrix if all values fit exactly; otherwise, return an empty matrix.

Example 1:

Convert 1D Array Into 2D Array, example 1: illustration of the example

Input: original = [5,6,7,8,9,10]
m = 2
n = 3

Output: [[5,6,7],[8,9,10]]

Explanation: The first three values form [5, 6, 7], and the next three form [8, 9, 10]. These two rows use every value in order.

Example 2:

Convert 1D Array Into 2D Array, example 2: illustration of the example

Input: original = [4,2,9]
m = 3
n = 1

Output: [[4],[2],[9]]

Explanation: Each row contains one value, so the rows are [4], [2], and [9], in the same order as the original array.

Example 3:

Convert 1D Array Into 2D Array, example 3: illustration of the example

Input: original = [1,2,3]
m = 2
n = 2

Output: []

Explanation: A matrix with two rows and two columns needs four values, but only three are available. Return an empty matrix.

Constraints:

  • 1 <= original.length <= 50000
  • 1 <= original[i] <= 100000
  • 1 <= m, n <= 40000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.