3546. Equal Sum Grid Partition I
Description
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 the elements in both sections is equal.
Return true
if such a partition exists; otherwise return false
.
Example 1:
Input: grid = [[1,4],[2,3]]
Output: true
Explanation:
A horizontal cut between row 0 and row 1 results in two non-empty sections, each with a sum of 5. Thus, the answer is true
.
Example 2:
Input: grid = [[1,3],[2,4]]
Output: false
Explanation:
No horizontal or vertical cut results in two non-empty sections with equal sums. Thus, 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
Solution 1: Enumeration + Prefix Sum
First, we calculate the sum of all elements in the matrix, denoted as \(s\). If \(s\) is odd, it is impossible to divide the matrix into two parts with equal sums, so we directly return false
.
If \(s\) is even, we can enumerate all possible partition lines to check if there exists a line that divides the matrix into two parts with equal sums.
We traverse each row from top to bottom, calculating the sum of all elements in the rows above the current row, denoted as \(\textit{pre}\). If \(\textit{pre} \times 2 = s\) and the current row is not the last row, it means we can perform a horizontal partition between the current row and the next row, so we return true
.
If no such partition line is found, we traverse each column from left to right, calculating the sum of all elements in the columns to the left of the current column, denoted as \(\textit{pre}\). If \(\textit{pre} \times 2 = s\) and the current column is not the last column, it means we can perform a vertical partition between the current column and the next column, so we return true
.
If no such partition line is found, we return false
.
The time complexity is \(O(m \times n)\), where \(m\) and \(n\) are the number of rows and columns in the matrix, respectively. The space complexity is \(O(1)\), as only constant extra space is used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|