leetcode 2906 构造乘积矩阵,矩阵递推

1.题目描述

2.解题思路和代码

参考灵神解题思路

class Solution:
    def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
        mod = 12345
        m, n = len(grid), len(grid[0])
        p = [[0] * n for _ in range(m)]

        suf = 1
        for i in range(m-1, -1, -1):
            for j in range(n-1, -1, -1):
                p[i][j] = suf
                suf = grid[i][j] * suf % mod
        
        pre = 1
        for i, row in enumerate(grid):
            for j, x in enumerate(row):
                p[i][j] = p[i][j] * pre % mod
                pre = pre * x % mod
        
        return p
04-22 20:05