浏览代码

#feat:leetcode p3546题题解

yangyi 2 周之前
父节点
当前提交
8a9c059b79
共有 2 个文件被更改,包括 146 次插入0 次删除
  1. 105 0
      src/leetcode/p3546/Solution.java
  2. 41 0
      src/leetcode/p3546/SolutionTest.java

+ 105 - 0
src/leetcode/p3546/Solution.java

@@ -0,0 +1,105 @@
+package leetcode.p3546;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/25 8:52
+ * @Description: https://leetcode.cn/problems/equal-sum-grid-partition-i/description/
+ * 3546. 等和矩阵分割 I
+ */
+public class Solution {
+    public boolean canPartitionGrid(int[][] grid) {
+        /**
+         * 枚举分割线
+         * 1.矩阵前部分和 是否等于 总和的一半
+         * 2.垂直分割时将矩阵旋转90度
+         */
+        long total = 0;
+        for (int[] ints : grid) {
+            for (int v : ints) {
+                total += v;
+            }
+        }
+        return check(grid,total) || check(rotate(grid),total);
+    }
+    //90度旋转数组
+    private int[][] rotate(int[][] grid) {
+        int m = grid.length, n = grid[0].length;
+        int[][] result = new int[n][m];
+        for (int i = 0; i < m; i++) {
+            for (int j = 0; j < n; j++) {
+                result[j][m - 1 - i] = grid[i][j];
+            }
+        }
+        return result;
+    }
+
+    private boolean check(int[][] grid, long total) {
+        long sum = 0;
+        for (int i = 0; i < grid.length-1; i++) {
+            for (int j = 0; j < grid[i].length; j++) {
+                sum +=grid[i][j];
+            }
+            if (sum*2 == total)return true;
+        }
+        return false;
+    }
+
+    private boolean prefixSolution(int[][] grid){
+        /**
+         * 1.求每行每列的和
+         * 2.判断行的和 和 列的和 能否被分为相等的两部分
+         * 前缀和 是否等于 后缀和
+         */
+        long[] row = new long[grid.length];
+        long[] col = new long[grid[0].length];
+        //compress row and col
+        for (int i = 0; i < grid.length; i++) {
+            for (int j = 0; j < grid[i].length; j++) {
+                //calculate row
+                if (j==0){
+                    row[i] = grid[i][j];
+                }else {
+                    row[i] += grid[i][j];
+                }
+                //calculate col
+                if (i==0){
+                    col[j] = grid[i][j];
+                }else {
+                    col[j] += grid[i][j];
+                }
+            }
+        }
+        //calculate prefix
+        long[] prefix_row = new long[row.length];
+        long[] prefix_col = new long[col.length];
+        prefix_row[0] = row[0];
+        prefix_col[0] = col[0];
+        for (int i = 1; i < row.length; i++) {
+            prefix_row[i] = prefix_row[i-1] + row[i];
+        }
+        for (int i = 1; i < col.length; i++) {
+            prefix_col[i] = prefix_col[i-1] + col[i];
+        }
+        //judge
+        if (prefix_row[prefix_row.length-1] % 2 == 0) {
+            for (int i = 0; i < prefix_row.length; i++) {
+                if (prefix_row[i] == (prefix_row[prefix_row.length-1]/2)){
+                    return true;
+                }else if (prefix_row[i] > (prefix_row[prefix_row.length-1]/2)){
+                    break;
+                }
+            }
+        }
+        if (prefix_col[prefix_col.length-1] % 2 == 0) {
+            for (int i = 0; i < prefix_col.length; i++) {
+                if (prefix_col[i] == (prefix_col[prefix_col.length-1]/2)){
+                    return true;
+                }else if (prefix_col[i] > (prefix_col[prefix_col.length-1]/2))
+                    break;
+            }
+        }
+        return false;
+    }
+}

+ 41 - 0
src/leetcode/p3546/SolutionTest.java

@@ -0,0 +1,41 @@
+package leetcode.p3546;
+
+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.assertEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/25 9:12
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private int[][] grid;
+    private boolean expected;
+
+    public SolutionTest(int[][] grid, boolean expected) {
+        this.grid = grid;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data(){
+        return Arrays.asList(new Object[][]{
+                {new int[][]{{1,4},{2,3}},true},
+                {new int[][]{{1,3},{2,4}},false},
+        });
+    }
+
+    @Test
+    public void canPartitionGrid() {
+        assertEquals(expected,solution.canPartitionGrid(grid));
+    }
+}