2022. Convert 1D Array Into 2D Array

Tapan RachchhTapan Rachchh
1 min read
class Solution:
    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:
        idealSize = len(original)

        if m * n != idealSize:
            return [] 

        arr = [[0 for _ in range(n)] for _ in range(m)]

        index = 0
        for i in range(m):
            for j in range(n):
                if index < idealSize:
                    arr[i][j] = original[index]
                    index += 1

        return arr
0
Subscribe to my newsletter

Read articles from Tapan Rachchh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tapan Rachchh
Tapan Rachchh