Jelajahi Sumber

#feat:leetcode p2906题题解

yangyi 2 minggu lalu
induk
melakukan
4c6bc1767d
2 mengubah file dengan 115 tambahan dan 0 penghapusan
  1. 72 0
      src/leetcode/p2906/Solution.java
  2. 43 0
      src/leetcode/p2906/SolutionTest.java

+ 72 - 0
src/leetcode/p2906/Solution.java

@@ -0,0 +1,72 @@
+package leetcode.p2906;
+
+import java.math.BigInteger;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/24 18:39
+ * @Description: https://leetcode.cn/problems/construct-product-matrix/description/
+ * 2906. 构造乘积矩阵
+ */
+public class Solution {
+    private final static int MOD = 12345;
+    public int[][] constructProductMatrix(int[][] grid) {
+        /**
+         * 前后缀分解
+         * <a href="https://leetcode.cn/discuss/post/3584387/fen-xiang-gun-mo-yun-suan-de-shi-jie-dan-7xgu/">为什么计算中途可以取模参考<a/>
+         */
+        int[][] ans = new int[grid.length][grid[0].length];
+        int val = 1;
+        //calculate prefix
+        for (int i = 0; i < grid.length; i++) {
+            for (int j = 0; j < grid[i].length; j++) {
+                ans[i][j] = val;
+                val = (val % MOD) * (grid[i][j] % MOD) % MOD;
+            }
+        }
+        //calculate suffix
+        val = 1;
+        for (int i = grid.length - 1; i >= 0; i--) {
+            for (int j = grid[i].length - 1; j >= 0; j--) {
+                //space optimize
+                //calculate ans and suffix
+                ans[i][j] = (ans[i][j] % MOD) * (val % MOD) % MOD;
+                val = (val % MOD) * (grid[i][j] % MOD) % MOD;
+            }
+        }
+        return ans;
+    }
+
+    /**
+     * 直接模拟
+     * @param grid
+     * @return {@code int[][] }
+     * @description:
+     * @author: 杨逸
+     * @data:2026/03/24 19:32:02
+     * @since 1.0.0
+     */
+    private int[][] simulateSolution(int[][] grid) {
+        /**
+         * 模拟
+         */
+        int[][] ans = new int[grid.length][grid[0].length];
+        BigInteger val = BigInteger.valueOf(1);
+        //long val  =1;
+        for (int i = 0; i < grid.length; i++) {
+            for (int j = 0; j < grid[i].length; j++) {
+                val = val.multiply(BigInteger.valueOf(grid[i][j]));
+                //val *= grid[i][j];
+            }
+        }
+        for (int i = 0; i < grid.length; i++) {
+            for (int j = 0; j < grid[i].length; j++) {
+                ans[i][j] = val.divide(BigInteger.valueOf(grid[i][j])).mod(BigInteger.valueOf(MOD)).intValue();
+                //ans[i][j] = (int) ((val / grid[i][j])%MOD);
+            }
+        }
+        return ans;
+    }
+}

+ 43 - 0
src/leetcode/p2906/SolutionTest.java

@@ -0,0 +1,43 @@
+package leetcode.p2906;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.junit.Assert.assertArrayEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/24 18:42
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private final int[][] grid;
+    private final int[][] expected;
+
+    public SolutionTest(int[][] grid, int[][] expected) {
+        this.grid = grid;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][]{
+                {new int[][]{{1,2},{3,4}},new int[][]{{24,12},{8,6}}},
+                {new int[][]{{12345},{2},{1}},new int[][]{{2},{0},{0}}},
+                {new int[][]{{10,20},{18,16},{17,14},{16,9},{14,6},{16,5},{14,8},{20,13},{16,10},{14,17}},new int[][]{{345,6345},{7050,4845},{4560,2010},{4845,1755},{2010,8805},{4845,690},{2010,9690},{6345,1215},{4845,345},{2010,4560}}},
+                {new int[][]{{414750857},{449145368},{767292749}},new int[][]{{1462},{3103},{9436}}},
+        });
+    }
+
+    @Test
+    public void constructProductMatrix() {
+        assertArrayEquals(expected,solution.constructProductMatrix(grid));
+    }
+}