Skip to Content
Leetcode3394. Check if Grid can be Cut into Sections

3394. Check if Grid can be Cut into Sections

3394. Check if Grid can be Cut into Sections

Medium

Array, Sorting

View on LeetCode

Intuition

The problem requires us to determine if a grid can be divided into sections based on given rectangles. Each rectangle is defined by its coordinates, and we need to check if there are gaps between these rectangles that would allow for a valid cut. The key insight is to identify these gaps along both the x-axis and y-axis. If there are at least two gaps in either dimension, it indicates that the grid can be cut into sections.

By sorting the rectangles based on their starting coordinates for each dimension, we can easily identify gaps between consecutive rectangles. This approach allows us to efficiently check if the grid can be divided into sections based on the given rectangles.

Approach

  1. Process Each Dimension Separately:

    • Check for gaps along the x-axis (dimension 0)
    • Check for gaps along the y-axis (dimension 1)
  2. For Each Dimension:

    • Sort rectangles based on their starting coordinate in that dimension
    • Initialize a variable to track the furthest end point encountered
    • Count gaps where the end of one rectangle doesn’t overlap with the start of the next
  3. Gap Identification:

    • A gap exists when the furthest end point is less than or equal to the start of the next rectangle
    • Update the furthest end point after processing each rectangle
  4. Determine Result:

    • Return true if there are at least two gaps in either dimension
    • Otherwise, return false

Code

/** * Function to check if there are gaps between rectangles along a specified dimension. * @param rectangles - An array of rectangles, where each rectangle is represented as [x1, y1, x2, y2]. * @param dim - The dimension to check for gaps (0 for x-axis, 1 for y-axis). * @returns True if there are at least two gaps, indicating the grid can be cut into sections. */ function checkGaps(rectangles: number[][], dim: number): boolean { // Initialize the gap count to zero let gapCount = 0; // Sort rectangles based on the starting point of the specified dimension rectangles.sort((a, b) => a[dim] - b[dim]); // Initialize furthestEnd to the end of the first rectangle in the specified dimension let furthestEnd = rectangles[0][dim + 2]; // Iterate through the sorted rectangles starting from the second one for (let i = 1; i < rectangles.length; i++) { const rect = rectangles[i]; // Check if there is a gap between furthestEnd and the start of the current rectangle if (furthestEnd <= rect[dim]) { gapCount++; } // Update furthestEnd to the maximum of its current value and the end of the current rectangle furthestEnd = Math.max(furthestEnd, rect[dim + 2]); } // Return true if there are at least two gaps return gapCount >= 2; } function checkValidCuts(n: number, rectangles: number[][]): boolean { // Return true if there is at least one valid cut in either dimension return checkGaps(rectangles, 0) || checkGaps(rectangles, 1); }

Time Complexity

  • Time Complexity: O(n log n)

    • n: number of rectangles
    • Sorting the rectangles takes O(n log n) time, which is the dominant factor
    • Iterating through the sorted rectangles takes O(n) time
  • Space Complexity: O(1)

    • We only use a constant amount of extra space regardless of input size
    • The sorting operation may require additional space depending on the implementation

This algorithm efficiently identifies if a grid can be cut into sections by processing rectangles in sorted order and checking for gaps between them.