You are given an m x n matrix grid of positive integers. Your task is to determine if it is possible to make either one horizontal or one vertical cut on the grid such that:
Each of the two resulting sections formed by the cut is non-empty.
The sum of elements in both sections is equal, or can be made equal by discounting at most one single cell in total (from either section).
If a cell is discounted, the rest of the section must remain connected.
Return true if such a partition exists; otherwise, return false.
Note: A section is connected if every cell in it can be reached from any other cell by moving up, down, left, or right through other cells in the section.
Β
Example 1:
Input:grid = [[1,4],[2,3]]
Output:true
Explanation:
A horizontal cut after the first row gives sums 1 + 4 = 5 and 2 + 3 = 5, which are equal. Thus, the answer is true.
Example 2:
Input:grid = [[1,2],[3,4]]
Output:true
Explanation:
A vertical cut after the first column gives sums 1 + 3 = 4 and 2 + 4 = 6.
By discounting 2 from the right section (6 - 2 = 4), both sections have equal sums and remain connected. Thus, the answer is true.
Example 3:
Input:grid = [[1,2,4],[2,3,5]]
Output:false
Explanation:
A horizontal cut after the first row gives 1 + 2 + 4 = 7 and 2 + 3 + 5 = 10.
By discounting 3 from the bottom section (10 - 3 = 7), both sections have equal sums, but they do not remain connected as it splits the bottom section into two parts ([2] and [5]). Thus, the answer is false.
Example 4:
Input:grid = [[4,1,8],[3,2,6]]
Output:false
Explanation:
No valid cut exists, so the answer is false.
Β
Constraints:
1 <= m == grid.length <= 105
1 <= n == grid[i].length <= 105
2 <= m * n <= 105
1 <= grid[i][j] <= 105
Solutions
Method 1: Enumerate Partition Lines
We can first enumerate horizontal partition lines, compute the element sum of each resulting part, and use hash maps to record the occurrence count of elements in each part.
For each partition line, we need to determine whether the sums of the two parts are equal, or whether removing one cell can make them equal. If the sums are equal, we directly return \(\text{true}\). If the sums are not equal, we compute their difference \(\textit{diff}\). If \(\textit{diff}\) exists in the hash map of the larger part and satisfies the connectivity condition after removing that cell, we also return \(\text{true}\).
The connectivity condition can be checked using the following criteria:
The part has more than \(1\) row and more than \(1\) column.
The part has exactly \(1\) row, and the removed cell is on the boundary of that part (i.e., the first column or the last column).
The part has exactly \(1\) row, and the removed cell is on the boundary of that part (i.e., the first row or the last row).
Satisfying any one of the above conditions guarantees that the part remains connected after removing the cell.
We also need to enumerate vertical partition lines, which is similar to the horizontal case. To simplify the enumeration of vertical partition lines, we can first transpose the matrix and then apply the same logic.
The time complexity is \(O(m \times n)\) and the space complexity is \(O(m \times n)\), where \(m\) and \(n\) are the number of rows and columns of the matrix, respectively.