Skip to content

Latest commit

 

History

History
31 lines (27 loc) · 1.46 KB

File metadata and controls

31 lines (27 loc) · 1.46 KB

Intuition

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.

Approach

  1. First, check if the total number of elements in the 1D array (original.length) matches m * n. If not, return an empty 2D array.
  2. Create a 2D array of size m x n.
  3. 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
  4. Return the constructed 2D array.

Complexity

  • 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 * n elements.

Code

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;
    }
}