Skip to content

Commit 5448915

Browse files
authored
update: 添加问题“2906.构造乘积矩阵”的代码和题解 (#1462)
* archive: modifies yesterday * clean: rename Signed-off-by: LetMeFly666 <Tisfy@qq.com> * word: en 2025.3.24 * 1886: AC,i=3break,100.00%,6.20% (#1458)(#1457) * 2906: CE.cpp (#1461) Line 18: Char 14: error: inline declaration of 'getIndex' not allowed in block scope 18 | auto inline getIndex = [&](const int& t) { | ^~~~~~ 1 error generated. * 2906: RE.cpp (#1461) - index err * 2906: AC.cpp (#1461) - AC,13.89%,8.33% * 2906: AC.cpp.O1 (#1461) - AC,61.11%,91.67% * update: 添加问题“2906.构造乘积矩阵”的代码和题解 (#1462) Signed-off-by: LetMeFly666 <Tisfy@qq.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 30b959a commit 5448915

17 files changed

Lines changed: 290 additions & 336 deletions

.commitmsg

Lines changed: 0 additions & 1 deletion
This file was deleted.

Codes/1594-maximum-non-negative-product-in-a-matrix.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* @Author: LetMeFly
33
* @Date: 2026-03-23 21:32:36
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-03-23 21:45:04
5+
* @LastEditTime: 2026-03-23 22:00:02
66
*/
77
#ifdef _DEBUG
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS CANNOT BE ACCEPTED
1211
typedef long long ll;
1312
const ll MOD = 1e9 + 7;
1413
class Solution {
@@ -17,21 +16,54 @@ class Solution {
1716
int n = grid.size(), m = grid[0].size();
1817
vector<vector<ll>> maximum(n, vector<ll>(m));
1918
vector<vector<ll>> minimum(n, vector<ll>(m));
20-
for (int i = 0; i < n; i++) {
21-
for (int j = 0; j < m; j++) {
22-
ll left_max = j ? maximum[i][j - 1] : 1;
23-
ll left_min = j ? minimum[i][j - 1] : 1;
24-
ll up_max = i ? maximum[i - 1][j] : 1;
25-
ll up_min = i ? minimum[i - 1][j] : 1;
19+
20+
maximum[0][0] = minimum[0][0] = grid[0][0];
21+
for (int i = 1; i < n; i++) {
22+
maximum[i][0] = minimum[i][0] = maximum[i - 1][0] * grid[i][0];
23+
}
24+
for (int j = 1; j < m; j++) {
25+
maximum[0][j] = minimum[0][j] = maximum[0][j - 1] * grid[0][j];
26+
}
27+
28+
for (int i = 1; i < n; i++) {
29+
for (int j = 1; j < m; j++) {
2630
if (grid[i][j] >= 0) {
27-
maximum[i][j] = max(left_max, up_max) * grid[i][j] % MOD;
28-
minimum[i][j] = max(left_min, up_min) * grid[i][j] % MOD;
31+
maximum[i][j] = max(maximum[i][j - 1], maximum[i - 1][j]) * grid[i][j];
32+
minimum[i][j] = min(minimum[i][j - 1], minimum[i - 1][j]) * grid[i][j];
2933
} else {
30-
maximum[i][j] = max(left_min, up_min) * grid[i][j] % MOD;
31-
minimum[i][j] = max(left_max, up_max) * grid[i][j] % MOD;
34+
maximum[i][j] = min(minimum[i][j - 1], minimum[i - 1][j]) * grid[i][j];
35+
minimum[i][j] = max(maximum[i][j - 1], maximum[i - 1][j]) * grid[i][j];
3236
}
3337
}
3438
}
35-
return maximum[n - 1][m - 1] >= 0 ? maximum[n - 1][m - 1] : -1;
39+
40+
return maximum[n - 1][m - 1] >= 0 ? maximum[n - 1][m - 1] % MOD : -1;
41+
}
42+
};
43+
44+
#ifdef _DEBUG
45+
/*
46+
[[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
47+
48+
之前的逻辑:
49+
ll left_max = j ? maximum[i][j - 1] : 1;
50+
ll left_min = j ? minimum[i][j - 1] : 1;
51+
ll up_max = i ? maximum[i - 1][j] : 1;
52+
ll up_min = i ? minimum[i - 1][j] : 1;
53+
minimum[i][j] = max(left_max, up_max) * grid[i][j]; // when grid[i][j] < 0
54+
错在:
55+
-1 -2
56+
在-2时候,min和max都应该是2,但是max(-1, 1)会导致计算结果为-2。
57+
58+
-1
59+
*/
60+
int main() {
61+
string s;
62+
while (cin >> s) {
63+
Solution sol;
64+
vector<vector<int>> v = stringToVectorVector(s);
65+
cout << sol.maxProductPath(v) << endl;
3666
}
37-
};
67+
return 0;
68+
}
69+
#endif

Codes/1594-maximum-non-negative-product-in-a-matrix_AC.cpp

Lines changed: 0 additions & 69 deletions
This file was deleted.

Codes/1886-determine-whether-matrix-can-be-obtained-by-rotation_AC.cpp renamed to Codes/1886-determine-whether-matrix-can-be-obtained-by-rotation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2026-03-22 23:35:27
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-03-22 23:45:34
5+
* @LastEditTime: 2026-03-24 21:44:44
66
*/
77
#ifdef _DEBUG
88
#include "_[1,2]toVector.h"
@@ -16,7 +16,7 @@ class Solution {
1616
if (mat == target) {
1717
return true;
1818
}
19-
if (t == 4) {
19+
if (t == 3) {
2020
break;
2121
}
2222
vector<vector<int>> tmp(n, vector<int>(n));

Codes/1886-determine-whether-matrix-can-be-obtained-by-rotation.cpp.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-24 21:47:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-24 22:02:21
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 12345;
13+
class Solution {
14+
public:
15+
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
16+
int n = grid.size(), m = grid[0].size();
17+
18+
auto getIndex = [&](const int& t) {
19+
return pair<int, int>{t / m, t % m};
20+
};
21+
22+
vector<vector<ll>> prefix(n, vector<ll>(m, 1));
23+
for (int t = 1; t < m * n; t++) {
24+
auto [i1, j1] = getIndex(t);
25+
auto [i0, j0] = getIndex(t - 1);
26+
prefix[i1][j1] = prefix[i0][j0] * grid[i0][j0] % MOD;
27+
}
28+
29+
vector<vector<ll>> suffix(n, vector<ll>(m, 1));
30+
for (int t = m * n - 2; t >= 0; t--) {
31+
auto [i0, j0] = getIndex(t);
32+
auto [i1, j1] = getIndex(t + 1);
33+
suffix[i0][j0] = suffix[i1][j1] * grid[i1][j1] % MOD;
34+
}
35+
36+
vector<vector<int>> ans(n, vector<int>(m, 1));
37+
for (int i = 0; i < n; i++) {
38+
for (int j = 0; j < m; j++) {
39+
ans[i][j] = prefix[i][j] * suffix[i][j] % MOD;
40+
}
41+
}
42+
return ans;
43+
}
44+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-24 22:02:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-24 22:07:50
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 12345;
13+
class Solution {
14+
public:
15+
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
16+
int n = grid.size(), m = grid[0].size();
17+
18+
auto getIndex = [&](const int& t) {
19+
return pair<int, int>{t / m, t % m};
20+
};
21+
22+
vector<vector<int>> ans(n, vector<int>(m, 1));
23+
for (int t = 1; t < m * n; t++) {
24+
auto [i1, j1] = getIndex(t);
25+
auto [i0, j0] = getIndex(t - 1);
26+
ans[i1][j1] = (ll)ans[i0][j0] * grid[i0][j0] % MOD;
27+
}
28+
29+
ll suffix = 1;
30+
for (int t = m * n - 2; t >= 0; t--) {
31+
auto [i0, j0] = getIndex(t);
32+
auto [i1, j1] = getIndex(t + 1);
33+
suffix = suffix * grid[i1][j1] % MOD;
34+
ans[i0][j0] = ans[i0][j0] * suffix % MOD;
35+
}
36+
return ans;
37+
}
38+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@
971971
|2894.分类求和并作差|简单|<a href="https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/27/LeetCode%202894.%E5%88%86%E7%B1%BB%E6%B1%82%E5%92%8C%E5%B9%B6%E4%BD%9C%E5%B7%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148266123" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/solutions/3687620/letmefly-2894fen-lei-qiu-he-bing-zuo-cha-suyc/" target="_blank">LeetCode题解</a>|
972972
|2900.最长相邻不相等子序列I|简单|<a href="https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/15/LeetCode%202900.%E6%9C%80%E9%95%BF%E7%9B%B8%E9%82%BB%E4%B8%8D%E7%9B%B8%E7%AD%89%E5%AD%90%E5%BA%8F%E5%88%97I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147990003" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/solutions/3677963/letmefly-2900zui-chang-xiang-lin-bu-xian-69ua/" target="_blank">LeetCode题解</a>|
973973
|2903.找出满足差值条件的下标I|简单|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/25/LeetCode%202903.%E6%89%BE%E5%87%BA%E6%BB%A1%E8%B6%B3%E5%B7%AE%E5%80%BC%E6%9D%A1%E4%BB%B6%E7%9A%84%E4%B8%8B%E6%A0%87I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139195914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/solutions/2789520/letmefly-2903zhao-chu-man-zu-chai-zhi-ti-m92q/" target="_blank">LeetCode题解</a>|
974+
|2906.构造乘积矩阵|中等|<a href="https://leetcode.cn/problems/construct-product-matrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/24/LeetCode%202906.%E6%9E%84%E9%80%A0%E4%B9%98%E7%A7%AF%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159436536" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/construct-product-matrix/solutions/3935999/letmefly-2906gou-zao-cheng-ji-ju-zhen-qi-g1ev/" target="_blank">LeetCode题解</a>|
974975
|2908.元素和最小的山形三元组I|简单|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/29/LeetCode%202908.%E5%85%83%E7%B4%A0%E5%92%8C%E6%9C%80%E5%B0%8F%E7%9A%84%E5%B1%B1%E5%BD%A2%E4%B8%89%E5%85%83%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137151595" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/solutions/2714346/letmefly-2908yuan-su-he-zui-xiao-de-shan-h3s9/" target="_blank">LeetCode题解</a>|
975976
|2917.找出数组中的K-or值|简单|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/06/LeetCode%202917.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84K-or%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136497896" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/solutions/2670279/letmefly-2917zhao-chu-shu-zu-zhong-de-k-4wuuh/" target="_blank">LeetCode题解</a>|
976977
|2918.数组的最小相等和|中等|<a href="https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/10/LeetCode%202918.%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%B0%8F%E7%9B%B8%E7%AD%89%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147858203" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/solutions/3673509/letmefly-2918shu-zu-de-zui-xiao-xiang-de-b7ha/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 1886.判断矩阵经轮转后是否一致.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public:
8080
if (mat == target) {
8181
return true;
8282
}
83-
if (t == 4) {
83+
if (t == 3) {
8484
break;
8585
}
8686
vector<vector<int>> tmp(n, vector<int>(n));

0 commit comments

Comments
 (0)