Leetcode Problem 2906. Construct Product Matrix

2906. Construct Product Matrix

Leetcode Solutions

Prefix and Suffix Product

  1. Initialize two arrays, rowProduct and colProduct, to store the prefix products for rows and columns respectively.
  2. Calculate the prefix product for each row and store it in rowProduct.
  3. Calculate the suffix product for each row in reverse order and multiply it with the corresponding value in rowProduct.
  4. Repeat steps 2 and 3 for columns, storing the results in colProduct.
  5. Initialize the result matrix p with the same dimensions as grid.
  6. For each element p[i][j] in the result matrix, calculate the product of rowProduct[i] and colProduct[j], excluding the current element grid[i][j], and take the modulo 12345.
  7. Return the result matrix p.
UML Thumbnail

Brute Force with Modulo Optimization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...