Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3548 - Equal Sum Grid Partition II.

    Hard

    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:

    • <code>1 <= m == grid.length <= 10<sup>5</sup></code>

    • <code>1 <= n == gridi.length <= 10<sup>5</sup></code>

    • <code>2 <= m * n <= 10<sup>5</sup></code>

    • <code>1 <= gridj<= 10<sup>5</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Boolean canPartitionGrid(Array<IntArray> grid)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait