W
What color is your bugatti?
@waytobugatti14 подп.
6просмотров
42.9%от подписчиков
24 марта 2026 г.
📷 ФотоScore: 7
#leetcode #medium #amazon 2906. Construct Product Matrix Дейлик литкода 24.03.2026 Given a 0-indexed 2D integer matrix grid of size n m, we define a 0-indexed 2D matrix p of size n m as the product matrix of grid if the following condition is met: Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo 12345. Return the product matrix of grid. Представляем матрицу как одномерный массив N = n m; // всего элементов Индекс idx = i m + j — это плоский номер клетки (i, j). Считаем префиксные произведения. Считаем суффиксные произведения. Для каждой клетки собираем ответ. class Solution { public int[][] constructProductMatrix(int[][] grid) { int n = grid.length; int m = grid[0].length; int N = n m; int MOD = 12345; long[] prefix = new long[N + 1]; prefix[0] = 1; for (int idx = 0; idx < N; idx++) { int i = idx / m; int j = idx % m; long val = (long) grid[i][j] % MOD; prefix[idx + 1] = prefix[idx] val % MOD; } long[] suffix = new long[N + 1]; suffix[N] = 1; for (int idx = N - 1; idx >= 0; idx--) { int i = idx / m; int j = idx % m; long val = (long) grid[i][j] % MOD; suffix[idx] = suffix[idx + 1] val % MOD; } int[][] p = new int[n][m]; for (int idx = 0; idx < N; idx++) { int i = idx / m; int j = idx % m; long prod = prefix[idx] suffix[idx + 1] % MOD; p[i][j] = (int) prod; } return p; } }
6
просмотров
1727
символов
Нет
эмодзи
Да
медиа

Другие посты @waytobugatti

Все посты канала →
#leetcode #medium #amazon 2906. Construct Product Matrix Дей — @waytobugatti | PostSniper