Given a collection of integer arrays arrays with different lengths, pad each array with zeros at the end so that all arrays have the same length, equal to the length of the longest array.
Return the resulting 2D matrix, in which every row has that uniform length.
Function Signature
def pad_matrix(arrays: list[list[int]]) -> list[list[int]]:
Rules
-
Rows stay in their original order, and each row keeps its original values in their original positions. Zeros are only added after the last element.
-
If every array is empty, the longest length is 0 and every row stays empty.
Constraints
-
1 <= len(arrays) <= 100
-
0 <= len(arrays[i]) <= 100
-
-10^4 <= arrays[i][j] <= 10^4
Examples
Input: arrays = [[1,2,3],[4,5],[6]]
Output: [[1,2,3],[4,5,0],[6,0,0]]
The longest array has length 3. [4,5] is padded to [4,5,0] and [6] is padded to [6,0,0].
Input: arrays = [[1],[2,3],[4,5,6],[7,8,9,10]]
Output: [[1,0,0,0],[2,3,0,0],[4,5,6,0],[7,8,9,10]]
The longest array has length 4. Each shorter array is padded with zeros to length 4, and the last row is unchanged.
Input: arrays = [[],[-3,7],[]]
Output: [[0,0],[-3,7],[0,0]]
The longest array has length 2, so each empty array becomes two zeros.