aboutsummaryrefslogtreecommitdiff
path: root/convert_1d_2d_arr.py
blob: 974bc50b0dfe931ac01cf0ddf3b3b4e9002f6785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 2022. Convert 1D Array Into 2D Array

"""
you are given a 0-indexed 1 dimensional integer array 'original' and two
integers 'm' and 'n'. you are tasked with creating a 2 dimensional array with
'm' rows and 'n' columns using all the elements from 'original'. the elements
from indices 0 to 'n - 1' (inclusively) of 'original' should form the first
row of the constructed 2d array, the elments from indices 'n' to '2 * n - 1'
should form the second row of the constructed 2d array, and so on. return an
'm * n' 2d array constructed according to the above procedure, or an empty 2d
array if it is not possible
"""


class Solution(object):
    def construct2DArray(self, original, m, n):
        """
        :type original: List[int]
        :type m: int
        :type n: int
        :rtype: List[List[int]]
        """
        ans = []
        if len(original) == m * n:
            for i in range(0, len(original), n):
                ans.append(original[i : i + n])
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.construct2DArray(original=[1, 2, 3, 4], m=2, n=2))
    print(obj.construct2DArray(original=[1, 2, 3], m=1, n=3))
    print(obj.construct2DArray(original=[1, 2], m=1, n=1))