Given two polynomials represented as integer arrays poly1 and poly2, return the coefficient array of their product.
Each array represents polynomial coefficients where the index corresponds to the power of x. For example, [3, 2, 1] represents the polynomial 3+2x+x2.
Return the resulting polynomial as an array of coefficients in the same format.
Function Signature
def multiply_polynomials(poly1: list[int], poly2: list[int]) -> list[int]:
Rules
-
poly1[i]
and
poly2[i]
are the coefficients of
xi
, so index 0 is the constant term.
-
The result has exactly
len(poly1) + len(poly2) - 1
entries, and entry
k
is the coefficient of
xk
in the product.
-
Keep every zero coefficient, including trailing ones. For example,
poly1 = [0]
and
poly2 = [4, 5]
give
[0, 0]
.
Constraints
-
1 <= len(poly1) <= 500
-
1 <= len(poly2) <= 500
-
-1000 <= poly1[i], poly2[i] <= 1000
Examples
Input: poly1 = [1, 2], poly2 = [3, 4]
Output: [3, 10, 8]
Here poly1 is 1+2x and poly2 is 3+4x. Expanding gives 3+4x+6x+8x2=3+10x+8x2, so the coefficients are [3, 10, 8].
Input: poly1 = [1, 1, 1], poly2 = [1, 1]
Output: [1, 2, 2, 1]
The product (1+x+x2)(1+x) expands to 1+x+x+x2+x2+x3=1+2x+2x2+x3.
Input: poly1 = [2, -1], poly2 = [-3, 0, 1]
Output: [-6, 3, 2, -1]
The product (2−x)(−3+x2) is −6+2x2+3x−x3. Ordered by power, that is [-6, 3, 2, -1], and the result has 2+3−1=4 entries.