Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3394 - Check if Grid can be Cut into Sections.

    Medium

    You are given an integer n representing the dimensions of an n x n grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where rectangles[i] is in the form <code>start<sub>x</sub>, start<sub>y</sub>, end<sub>x</sub>, end<sub>y</sub></code>, representing a rectangle on the grid. Each rectangle is defined as follows:

    • <code>(start<sub>x</sub>, start<sub>y</sub>)</code>: The bottom-left corner of the rectangle.

    • <code>(end<sub>x</sub>, end<sub>y</sub>)</code>: The top-right corner of the rectangle.

    Note that the rectangles do not overlap. Your task is to determine if it is possible to make either two horizontal or two vertical cuts on the grid such that:

    • Each of the three resulting sections formed by the cuts contains at least one rectangle.

    • Every rectangle belongs to exactly one section.

    Return true if such cuts can be made; otherwise, return false.

    Example 1:

    Input: n = 5, rectangles = \[\[1,0,5,2],0,2,2,4,3,2,5,3,0,4,4,5]

    Output: true

    Explanation:

    The grid is shown in the diagram. We can make horizontal cuts at y = 2 and y = 4. Hence, output is true.

    Example 2:

    Input: n = 4, rectangles = \[\[0,0,1,1],2,0,3,4,0,2,2,3,3,0,4,3]

    Output: true

    Explanation:

    We can make vertical cuts at x = 2 and x = 3. Hence, output is true.

    Example 3:

    Input: n = 4, rectangles = \[\[0,2,2,4],1,0,3,2,2,2,3,4,3,0,4,2,3,2,4,4]

    Output: false

    Explanation:

    We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.

    Constraints:

    • <code>3 <= n <= 10<sup>9</sup></code>

    • <code>3 <= rectangles.length <= 10<sup>5</sup></code>

    • 0 &lt;= rectangles[i][0] &lt; rectangles[i][2] &lt;= n

    • 0 &lt;= rectangles[i][1] &lt; rectangles[i][3] &lt;= n

    • No two rectangles overlap.

    • 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 checkValidCuts(Integer m, Array<IntArray> rectangles)
      • Methods inherited from class java.lang.Object

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