Show problems as
Classic problem #3546MediumArrayMatrixEnumerationPrefix Sum

Equal Sum Grid Partition I

Real-world version: Splitting the Yield Plot in Two
Tracked in this browser only
Read Equal Sum Grid Partition I on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A field is surveyed as plot, a grid of parcels where plot[r][c] is the yield of one parcel. Every yield is at least 1.

The field is to be divided between two tenants by a single straight fence. The fence runs the whole way across the field, either along a line between two rows of parcels or along a line between two columns, and it never cuts through a parcel. Both sides must get at least one parcel.

Return true if some such fence gives the two sides the same total yield, and false otherwise.

Examples

Example 1

Input
plot = [[4, 1], [2, 3]]
Output
true

A fence between the two rows gives the top side 4 + 1 = 5 and the bottom side 2 + 3 = 5.

Example 2

Input
plot = [[3, 1], [2, 4]]
Output
true

A fence between the two columns gives the left side 3 + 2 = 5 and the right side 1 + 4 = 5.

Example 3

Input
plot = [[2, 1], [1, 4]]
Output
false

The whole field yields 8. The fence between the rows gives 3 and 5, and the fence between the columns gives 3 and 5, so no fence splits it evenly.

Constraints

  • 1 <= rows == plot.length <= 10^5
  • 1 <= cols == plot[r].length <= 10^5
  • 2 <= rows * cols <= 10^5
  • 1 <= plot[r][c] <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def can_split_plot(plot: list[list[int]]) -> bool:
Java
public boolean canSplitPlot(int[][] plot)