Skip to content

Commit d7a0a5b

Browse files
authored
feat: add solutions to lc problem: No.2906 (doocs#1826)
No.2906.Construct Product Matrix
1 parent 1946da0 commit d7a0a5b

File tree

8 files changed

+435
-6
lines changed

8 files changed

+435
-6
lines changed

solution/2900-2999/2906.Construct Product Matrix/README.md

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,183 @@ p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0 ,所
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:前后缀分解**
56+
57+
我们可以预处理出每个元素的后缀乘积(不包含自身),然后再遍历矩阵,计算得到每个元素的前缀乘积(不包含自身),将两者相乘即可得到每个位置的结果。
58+
59+
具体地,我们用 $p[i][j]$ 表示矩阵中第 $i$ 行第 $j$ 列元素的结果,定义一个变量 $suf$ 表示当前位置右下方的所有元素的乘积,初始时 $suf = 1$。我们从矩阵右下角开始遍历,对于每个位置 $(i, j)$,我们将 $suf$ 赋值给 $p[i][j]$,然后更新 $suf$ 为 $suf \times grid[i][j] \bmod 12345$,这样就可以得到每个位置的后缀乘积。
60+
61+
接下来我们从矩阵左上角开始遍历,对于每个位置 $(i, j)$,我们将 $p[i][j]$ 乘上 $pre$,再对 $12345$ 取模,然后更新 $pre$ 为 $pre \times grid[i][j] \bmod 12345$,这样就可以得到每个位置的前缀乘积。
62+
63+
遍历结束,返回结果矩阵 $p$ 即可。
64+
65+
时间复杂度 $O(n \times m)$,其中 $n$ 和 $m$ 分别是矩阵的行数和列数。忽略结果矩阵的空间占用,空间复杂度 $O(1)$。
66+
5567
<!-- tabs:start -->
5668

5769
### **Python3**
5870

5971
<!-- 这里可写当前语言的特殊实现逻辑 -->
6072

6173
```python
62-
74+
class Solution:
75+
def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
76+
n, m = len(grid), len(grid[0])
77+
p = [[0] * m for _ in range(n)]
78+
mod = 12345
79+
suf = 1
80+
for i in range(n - 1, -1, -1):
81+
for j in range(m - 1, -1, -1):
82+
p[i][j] = suf
83+
suf = suf * grid[i][j] % mod
84+
pre = 1
85+
for i in range(n):
86+
for j in range(m):
87+
p[i][j] = p[i][j] * pre % mod
88+
pre = pre * grid[i][j] % mod
89+
return p
6390
```
6491

6592
### **Java**
6693

6794
<!-- 这里可写当前语言的特殊实现逻辑 -->
6895

6996
```java
70-
97+
class Solution {
98+
public int[][] constructProductMatrix(int[][] grid) {
99+
final int mod = 12345;
100+
int n = grid.length, m = grid[0].length;
101+
int[][] p = new int[n][m];
102+
long suf = 1;
103+
for (int i = n - 1; i >= 0; --i) {
104+
for (int j = m - 1; j >= 0; --j) {
105+
p[i][j] = (int) suf;
106+
suf = suf * grid[i][j] % mod;
107+
}
108+
}
109+
long pre = 1;
110+
for (int i = 0; i < n; ++i) {
111+
for (int j = 0; j < m; ++j) {
112+
p[i][j] = (int) (p[i][j] * pre % mod);
113+
pre = pre * grid[i][j] % mod;
114+
}
115+
}
116+
return p;
117+
}
118+
}
71119
```
72120

73121
### **C++**
74122

75123
```cpp
76-
124+
class Solution {
125+
public:
126+
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
127+
const int mod = 12345;
128+
int n = grid.size(), m = grid[0].size();
129+
vector<vector<int>> p(n, vector<int>(m));
130+
long long suf = 1;
131+
for (int i = n - 1; i >= 0; --i) {
132+
for (int j = m - 1; j >= 0; --j) {
133+
p[i][j] = suf;
134+
suf = suf * grid[i][j] % mod;
135+
}
136+
}
137+
long long pre = 1;
138+
for (int i = 0; i < n; ++i) {
139+
for (int j = 0; j < m; ++j) {
140+
p[i][j] = p[i][j] * pre % mod;
141+
pre = pre * grid[i][j] % mod;
142+
}
143+
}
144+
return p;
145+
}
146+
};
77147
```
78148
79149
### **Go**
80150
81151
```go
152+
func constructProductMatrix(grid [][]int) [][]int {
153+
const mod int = 12345
154+
n, m := len(grid), len(grid[0])
155+
p := make([][]int, n)
156+
for i := range p {
157+
p[i] = make([]int, m)
158+
}
159+
suf := 1
160+
for i := n - 1; i >= 0; i-- {
161+
for j := m - 1; j >= 0; j-- {
162+
p[i][j] = suf
163+
suf = suf * grid[i][j] % mod
164+
}
165+
}
166+
pre := 1
167+
for i := 0; i < n; i++ {
168+
for j := 0; j < m; j++ {
169+
p[i][j] = p[i][j] * pre % mod
170+
pre = pre * grid[i][j] % mod
171+
}
172+
}
173+
return p
174+
}
175+
```
176+
177+
### **TypeScript**
178+
179+
```ts
180+
function constructProductMatrix(grid: number[][]): number[][] {
181+
const mod = 12345;
182+
const [n, m] = [grid.length, grid[0].length];
183+
const p: number[][] = Array.from({ length: n }, () => Array.from({ length: m }, () => 0));
184+
let suf = 1;
185+
for (let i = n - 1; ~i; --i) {
186+
for (let j = m - 1; ~j; --j) {
187+
p[i][j] = suf;
188+
suf = (suf * grid[i][j]) % mod;
189+
}
190+
}
191+
let pre = 1;
192+
for (let i = 0; i < n; ++i) {
193+
for (let j = 0; j < m; ++j) {
194+
p[i][j] = (p[i][j] * pre) % mod;
195+
pre = (pre * grid[i][j]) % mod;
196+
}
197+
}
198+
return p;
199+
}
200+
```
82201

202+
### **Rust**
203+
204+
```rust
205+
impl Solution {
206+
pub fn construct_product_matrix(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
207+
let modulo: i32 = 12345;
208+
let n = grid.len();
209+
let m = grid[0].len();
210+
let mut p: Vec<Vec<i32>> = vec![vec![0; m]; n];
211+
let mut suf = 1;
212+
213+
for i in (0..n).rev() {
214+
for j in (0..m).rev() {
215+
p[i][j] = suf;
216+
suf = (suf as i64 * grid[i][j] as i64 % modulo as i64) as i32;
217+
}
218+
}
219+
220+
let mut pre = 1;
221+
222+
for i in 0..n {
223+
for j in 0..m {
224+
p[i][j] = (p[i][j] as i64 * pre as i64 % modulo as i64) as i32;
225+
pre = (pre as i64 * grid[i][j] as i64 % modulo as i64) as i32;
226+
}
227+
}
228+
229+
p
230+
}
231+
}
83232
```
84233

85234
### **...**

solution/2900-2999/2906.Construct Product Matrix/README_EN.md

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,179 @@ So the answer is [[2],[0],[0]].</pre>
4646

4747
## Solutions
4848

49+
**Solution 1: Prefix and Suffix Decomposition**
50+
51+
We can preprocess the suffix product (excluding itself) of each element, and then traverse the matrix to calculate the prefix product (excluding itself) of each element. The product of the two gives us the result for each position.
52+
53+
Specifically, we use $p[i][j]$ to represent the result of the element in the $i$-th row and $j$-th column of the matrix. We define a variable $suf$ to represent the product of all elements below and to the right of the current position. Initially, $suf$ is set to $1$. We start traversing from the bottom right corner of the matrix. For each position $(i, j)$, we assign $suf$ to $p[i][j]$, and then update $suf$ to $suf \times grid[i][j] \bmod 12345$. This way, we can obtain the suffix product of each position.
54+
55+
Next, we start traversing from the top left corner of the matrix. For each position $(i, j)$, we multiply $p[i][j]$ by $pre$, take the result modulo $12345$, and then update $pre$ to $pre \times grid[i][j] \bmod 12345$. This way, we can obtain the prefix product of each position.
56+
57+
After the traversal, we return the result matrix $p$.
58+
59+
The time complexity is $O(n \times m)$, where $n$ and $m$ are the number of rows and columns in the matrix, respectively. Ignoring the space occupied by the result matrix, the space complexity is $O(1)$.
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**
5264

5365
```python
54-
66+
class Solution:
67+
def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
68+
n, m = len(grid), len(grid[0])
69+
p = [[0] * m for _ in range(n)]
70+
mod = 12345
71+
suf = 1
72+
for i in range(n - 1, -1, -1):
73+
for j in range(m - 1, -1, -1):
74+
p[i][j] = suf
75+
suf = suf * grid[i][j] % mod
76+
pre = 1
77+
for i in range(n):
78+
for j in range(m):
79+
p[i][j] = p[i][j] * pre % mod
80+
pre = pre * grid[i][j] % mod
81+
return p
5582
```
5683

5784
### **Java**
5885

5986
```java
60-
87+
class Solution {
88+
public int[][] constructProductMatrix(int[][] grid) {
89+
final int mod = 12345;
90+
int n = grid.length, m = grid[0].length;
91+
int[][] p = new int[n][m];
92+
long suf = 1;
93+
for (int i = n - 1; i >= 0; --i) {
94+
for (int j = m - 1; j >= 0; --j) {
95+
p[i][j] = (int) suf;
96+
suf = suf * grid[i][j] % mod;
97+
}
98+
}
99+
long pre = 1;
100+
for (int i = 0; i < n; ++i) {
101+
for (int j = 0; j < m; ++j) {
102+
p[i][j] = (int) (p[i][j] * pre % mod);
103+
pre = pre * grid[i][j] % mod;
104+
}
105+
}
106+
return p;
107+
}
108+
}
61109
```
62110

63111
### **C++**
64112

65113
```cpp
66-
114+
class Solution {
115+
public:
116+
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
117+
const int mod = 12345;
118+
int n = grid.size(), m = grid[0].size();
119+
vector<vector<int>> p(n, vector<int>(m));
120+
long long suf = 1;
121+
for (int i = n - 1; i >= 0; --i) {
122+
for (int j = m - 1; j >= 0; --j) {
123+
p[i][j] = suf;
124+
suf = suf * grid[i][j] % mod;
125+
}
126+
}
127+
long long pre = 1;
128+
for (int i = 0; i < n; ++i) {
129+
for (int j = 0; j < m; ++j) {
130+
p[i][j] = p[i][j] * pre % mod;
131+
pre = pre * grid[i][j] % mod;
132+
}
133+
}
134+
return p;
135+
}
136+
};
67137
```
68138
69139
### **Go**
70140
71141
```go
142+
func constructProductMatrix(grid [][]int) [][]int {
143+
const mod int = 12345
144+
n, m := len(grid), len(grid[0])
145+
p := make([][]int, n)
146+
for i := range p {
147+
p[i] = make([]int, m)
148+
}
149+
suf := 1
150+
for i := n - 1; i >= 0; i-- {
151+
for j := m - 1; j >= 0; j-- {
152+
p[i][j] = suf
153+
suf = suf * grid[i][j] % mod
154+
}
155+
}
156+
pre := 1
157+
for i := 0; i < n; i++ {
158+
for j := 0; j < m; j++ {
159+
p[i][j] = p[i][j] * pre % mod
160+
pre = pre * grid[i][j] % mod
161+
}
162+
}
163+
return p
164+
}
165+
```
166+
167+
### **TypeScript**
168+
169+
```ts
170+
function constructProductMatrix(grid: number[][]): number[][] {
171+
const mod = 12345;
172+
const [n, m] = [grid.length, grid[0].length];
173+
const p: number[][] = Array.from({ length: n }, () => Array.from({ length: m }, () => 0));
174+
let suf = 1;
175+
for (let i = n - 1; ~i; --i) {
176+
for (let j = m - 1; ~j; --j) {
177+
p[i][j] = suf;
178+
suf = (suf * grid[i][j]) % mod;
179+
}
180+
}
181+
let pre = 1;
182+
for (let i = 0; i < n; ++i) {
183+
for (let j = 0; j < m; ++j) {
184+
p[i][j] = (p[i][j] * pre) % mod;
185+
pre = (pre * grid[i][j]) % mod;
186+
}
187+
}
188+
return p;
189+
}
190+
```
72191

192+
### **Rust**
193+
194+
```rust
195+
impl Solution {
196+
pub fn construct_product_matrix(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
197+
let modulo: i32 = 12345;
198+
let n = grid.len();
199+
let m = grid[0].len();
200+
let mut p: Vec<Vec<i32>> = vec![vec![0; m]; n];
201+
let mut suf = 1;
202+
203+
for i in (0..n).rev() {
204+
for j in (0..m).rev() {
205+
p[i][j] = suf;
206+
suf = (suf as i64 * grid[i][j] as i64 % modulo as i64) as i32;
207+
}
208+
}
209+
210+
let mut pre = 1;
211+
212+
for i in 0..n {
213+
for j in 0..m {
214+
p[i][j] = (p[i][j] as i64 * pre as i64 % modulo as i64) as i32;
215+
pre = (pre as i64 * grid[i][j] as i64 % modulo as i64) as i32;
216+
}
217+
}
218+
219+
p
220+
}
221+
}
73222
```
74223

75224
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
4+
const int mod = 12345;
5+
int n = grid.size(), m = grid[0].size();
6+
vector<vector<int>> p(n, vector<int>(m));
7+
long long suf = 1;
8+
for (int i = n - 1; i >= 0; --i) {
9+
for (int j = m - 1; j >= 0; --j) {
10+
p[i][j] = suf;
11+
suf = suf * grid[i][j] % mod;
12+
}
13+
}
14+
long long pre = 1;
15+
for (int i = 0; i < n; ++i) {
16+
for (int j = 0; j < m; ++j) {
17+
p[i][j] = p[i][j] * pre % mod;
18+
pre = pre * grid[i][j] % mod;
19+
}
20+
}
21+
return p;
22+
}
23+
};

0 commit comments

Comments
 (0)