The problem requires us to construct a 2D array from a 1D array. My initial thought is to check whether it's possible to form such a 2D array based on the given dimensions m and n. If the total number of elements in the original array is not equal to m * n, then it's not possible to form the 2D array, and we should return an empty array. Otherwise, we can map each element of the 1D array to the corresponding position in the 2D array.
- First, check if the total number of elements in the 1D array (
original.length) matchesm * n. If not, return an empty 2D array. - Create a 2D array of size
m x n. - Iterate over the elements of the original 1D array and place each element in its corresponding position in the 2D array using the index mapping:
- Row index:
i / n - Column index:
i % n
- Row index:
- Return the constructed 2D array.
-
Time complexity: The time complexity is O(m × n) because we iterate over all elements of the original array exactly once.
-
Space complexity: The space complexity is O(m × n) as we are creating a new 2D array with
m * nelements.
class Solution {
public int[][] construct2DArray(int[] original, int m, int n) {
if (m * n != original.length) {
return new int[0][0];
}
int[][] result = new int[m][n];
for (int i = 0; i < original.length; i++) {
result[i / n][i % n] = original[i];
}
return result;
}
}