Solution.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package leetcode.p3548;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. /**
  5. * @ProjectName: LeetCode
  6. * @FileName: Solution
  7. * @Author: 杨逸
  8. * @Data:2026/3/26 18:07
  9. * @Description: https://leetcode.cn/problems/equal-sum-grid-partition-ii/description/
  10. * 3548. 等和矩阵分割 II
  11. */
  12. public class Solution {
  13. public boolean canPartitionGrid(int[][] grid) {
  14. /**
  15. * 枚举分界线
  16. * 当前值sum大于total/2时,判断能否通过减去一个单元格使的sum=total/2
  17. * 使用两个哈希表记录分界线前后的元素
  18. * 垂直分割时将矩阵旋转即可
  19. * 当只有一列时,只有第一行和最后一行可以减去
  20. * 只有一行时,只能减去这行的两端
  21. */
  22. long total = 0;
  23. Map<Long,Integer> suffixMap = new HashMap<>();
  24. for (int[] ints : grid) {
  25. for (int v : ints) {
  26. total += v;
  27. Integer count = suffixMap.getOrDefault((long)v, 0);
  28. count++;
  29. suffixMap.put((long)v,count);
  30. }
  31. }
  32. return check(grid,total, new HashMap<>(suffixMap)) || check(rotate(grid),total,new HashMap<>(suffixMap));
  33. }
  34. private boolean check(int[][] grid, long total, Map<Long, Integer> suffixMap) {
  35. Map<Long,Integer> prefixMap = new HashMap<>();
  36. long sum = 0;
  37. for (int i = 0; i < grid.length-1; i++) {
  38. for (int j = 0; j < grid[0].length; j++) {
  39. int v = grid[i][j];
  40. sum += v;
  41. Integer count = prefixMap.getOrDefault((long)v, 0);
  42. count++;
  43. prefixMap.put((long)v,count);
  44. count = suffixMap.get((long)v) - 1;
  45. if (count == 0) {
  46. suffixMap.remove((long)v);
  47. } else {
  48. suffixMap.put((long)v,count);
  49. }
  50. }
  51. if (sum * 2 == total) {
  52. //just right
  53. return true;
  54. }else if (sum * 2 > total) {
  55. //subtract prefix
  56. long temp = total - sum;
  57. long d = sum - temp;
  58. if (grid[0].length == 1){
  59. //only one colum
  60. if (grid[0][0] == d || grid[i][0] == d) {
  61. return true;
  62. }
  63. }else if(i==0){
  64. //only one line
  65. if (grid[0][0] == d || grid[0][grid[0].length-1] == d) {
  66. return true;
  67. }
  68. } else{
  69. if (!prefixMap.getOrDefault(Long.valueOf(d +""),0).equals(0)) {
  70. return true;
  71. }
  72. }
  73. }else{
  74. //subtract suffix
  75. long temp = total - sum;
  76. long d = temp - sum;
  77. if (grid[0].length == 1) {
  78. if (grid[i+1][0] == d || grid[grid.length-1][0] == d) {
  79. return true;
  80. }
  81. }else if(i==grid.length-2){
  82. if (grid[i+1][0] == d || grid[i+1][grid[0].length-1] == d) {
  83. return true;
  84. }
  85. } else{
  86. if (!suffixMap.getOrDefault(Long.valueOf(d +""),0).equals(0)) {
  87. return true;
  88. }
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. private int[][] rotate(int[][] grid) {
  95. int m = grid.length, n = grid[0].length;
  96. int[][] result = new int[n][m];
  97. for (int i = 0; i < m; i++) {
  98. for (int j = 0; j < n; j++) {
  99. result[j][m - 1 - i] = grid[i][j];
  100. }
  101. }
  102. return result;
  103. }
  104. }