From 69ef873902481e78e619484aff163cc4219d92b5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 13 Mar 2026 07:44:40 +0800 Subject: [PATCH] feat: add solutions for lc No.2906 --- .../2906.Construct Product Matrix/README.md | 62 +++++++++++++++++++ .../README_EN.md | 62 +++++++++++++++++++ .../2906.Construct Product Matrix/Solution.cs | 28 +++++++++ .../2906.Construct Product Matrix/Solution.js | 24 +++++++ 4 files changed, 176 insertions(+) create mode 100644 solution/2900-2999/2906.Construct Product Matrix/Solution.cs create mode 100644 solution/2900-2999/2906.Construct Product Matrix/Solution.js diff --git a/solution/2900-2999/2906.Construct Product Matrix/README.md b/solution/2900-2999/2906.Construct Product Matrix/README.md index 1166dce490b89..28be37bbee779 100644 --- a/solution/2900-2999/2906.Construct Product Matrix/README.md +++ b/solution/2900-2999/2906.Construct Product Matrix/README.md @@ -243,6 +243,68 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} grid + * @return {number[][]} + */ +var constructProductMatrix = function (grid) { + const mod = 12345; + const [n, m] = [grid.length, grid[0].length]; + const p = Array.from({ length: n }, () => Array.from({ length: m }, () => 0)); + let suf = 1; + for (let i = n - 1; ~i; --i) { + for (let j = m - 1; ~j; --j) { + p[i][j] = suf; + suf = (suf * grid[i][j]) % mod; + } + } + let pre = 1; + for (let i = 0; i < n; ++i) { + for (let j = 0; j < m; ++j) { + p[i][j] = (p[i][j] * pre) % mod; + pre = (pre * grid[i][j]) % mod; + } + } + return p; +}; +``` + +#### C# + +```cs +public class Solution { + public int[][] ConstructProductMatrix(int[][] grid) { + const int mod = 12345; + int n = grid.Length, m = grid[0].Length; + int[][] p = new int[n][]; + for (int i = 0; i < n; ++i) { + p[i] = new int[m]; + } + + long suf = 1; + for (int i = n - 1; i >= 0; --i) { + for (int j = m - 1; j >= 0; --j) { + p[i][j] = (int)suf; + suf = suf * grid[i][j] % mod; + } + } + + long pre = 1; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + p[i][j] = (int)(p[i][j] * pre % mod); + pre = pre * grid[i][j] % mod; + } + } + + return p; + } +} +``` + diff --git a/solution/2900-2999/2906.Construct Product Matrix/README_EN.md b/solution/2900-2999/2906.Construct Product Matrix/README_EN.md index 198acbcd2c94a..9eb839ef8352f 100644 --- a/solution/2900-2999/2906.Construct Product Matrix/README_EN.md +++ b/solution/2900-2999/2906.Construct Product Matrix/README_EN.md @@ -241,6 +241,68 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} grid + * @return {number[][]} + */ +var constructProductMatrix = function (grid) { + const mod = 12345; + const [n, m] = [grid.length, grid[0].length]; + const p = Array.from({ length: n }, () => Array.from({ length: m }, () => 0)); + let suf = 1; + for (let i = n - 1; ~i; --i) { + for (let j = m - 1; ~j; --j) { + p[i][j] = suf; + suf = (suf * grid[i][j]) % mod; + } + } + let pre = 1; + for (let i = 0; i < n; ++i) { + for (let j = 0; j < m; ++j) { + p[i][j] = (p[i][j] * pre) % mod; + pre = (pre * grid[i][j]) % mod; + } + } + return p; +}; +``` + +#### C# + +```cs +public class Solution { + public int[][] ConstructProductMatrix(int[][] grid) { + const int mod = 12345; + int n = grid.Length, m = grid[0].Length; + int[][] p = new int[n][]; + for (int i = 0; i < n; ++i) { + p[i] = new int[m]; + } + + long suf = 1; + for (int i = n - 1; i >= 0; --i) { + for (int j = m - 1; j >= 0; --j) { + p[i][j] = (int)suf; + suf = suf * grid[i][j] % mod; + } + } + + long pre = 1; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + p[i][j] = (int)(p[i][j] * pre % mod); + pre = pre * grid[i][j] % mod; + } + } + + return p; + } +} +``` + diff --git a/solution/2900-2999/2906.Construct Product Matrix/Solution.cs b/solution/2900-2999/2906.Construct Product Matrix/Solution.cs new file mode 100644 index 0000000000000..812a04ec3fce7 --- /dev/null +++ b/solution/2900-2999/2906.Construct Product Matrix/Solution.cs @@ -0,0 +1,28 @@ +public class Solution { + public int[][] ConstructProductMatrix(int[][] grid) { + const int mod = 12345; + int n = grid.Length, m = grid[0].Length; + int[][] p = new int[n][]; + for (int i = 0; i < n; ++i) { + p[i] = new int[m]; + } + + long suf = 1; + for (int i = n - 1; i >= 0; --i) { + for (int j = m - 1; j >= 0; --j) { + p[i][j] = (int)suf; + suf = suf * grid[i][j] % mod; + } + } + + long pre = 1; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + p[i][j] = (int)(p[i][j] * pre % mod); + pre = pre * grid[i][j] % mod; + } + } + + return p; + } +} diff --git a/solution/2900-2999/2906.Construct Product Matrix/Solution.js b/solution/2900-2999/2906.Construct Product Matrix/Solution.js new file mode 100644 index 0000000000000..b30b443cc9a85 --- /dev/null +++ b/solution/2900-2999/2906.Construct Product Matrix/Solution.js @@ -0,0 +1,24 @@ +/** + * @param {number[][]} grid + * @return {number[][]} + */ +var constructProductMatrix = function (grid) { + const mod = 12345; + const [n, m] = [grid.length, grid[0].length]; + const p = Array.from({ length: n }, () => Array.from({ length: m }, () => 0)); + let suf = 1; + for (let i = n - 1; ~i; --i) { + for (let j = m - 1; ~j; --j) { + p[i][j] = suf; + suf = (suf * grid[i][j]) % mod; + } + } + let pre = 1; + for (let i = 0; i < n; ++i) { + for (let j = 0; j < m; ++j) { + p[i][j] = (p[i][j] * pre) % mod; + pre = (pre * grid[i][j]) % mod; + } + } + return p; +};