LeetCode 3394: Check if Grid can be Cut into Sections Solution

Master LeetCode problem 3394 (Check if Grid can be Cut into Sections), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3394. Check if Grid can be Cut into Sections

Problem Explanation

Explanation:

To solve this problem, we can iterate over all the rectangles and find the ranges of x and y coordinates covered by them. Then, we can check if it is possible to make either two horizontal or two vertical cuts on the grid such that each resulting section contains at least one rectangle.

  1. Iterate over all rectangles to find the x and y ranges covered by the rectangles.
  2. Check if it is possible to make two horizontal or two vertical cuts that satisfy the conditions.

Time Complexity: O(n) where n is the number of rectangles as we iterate over all rectangles once. Space Complexity: O(n) to store the x and y ranges covered by the rectangles.

:

Solution Code

class Solution {
    public boolean isRectangleCover(int n, int[][] rectangles) {
        int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE, maxY = Integer.MIN_VALUE;
        Set<String> points = new HashSet<>();

        int totalArea = 0;
        for (int[] rect : rectangles) {
            minX = Math.min(minX, rect[0]);
            minY = Math.min(minY, rect[1]);
            maxX = Math.max(maxX, rect[2]);
            maxY = Math.max(maxY, rect[3]);

            totalArea += (rect[2] - rect[0]) * (rect[3] - rect[1]);

            String bottomLeft = rect[0] + " " + rect[1];
            String topLeft = rect[0] + " " + rect[3];
            String topRight = rect[2] + " " + rect[3];
            String bottomRight = rect[2] + " " + rect[1];

            if (!points.add(bottomLeft)) points.remove(bottomLeft);
            if (!points.add(topLeft)) points.remove(topLeft);
            if (!points.add(topRight)) points.remove(topRight);
            if (!points.add(bottomRight)) points.remove(bottomRight);
        }

        if (!points.contains(minX + " " + minY) || !points.contains(minX + " " + maxY) || !points.contains(maxX + " " + minY) || !points.contains(maxX + " " + maxY) || points.size() != 4)
            return false;

        return totalArea == (maxX - minX) * (maxY - minY);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3394 (Check if Grid can be Cut into Sections)?

This page provides optimized solutions for LeetCode problem 3394 (Check if Grid can be Cut into Sections) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3394 (Check if Grid can be Cut into Sections)?

The time complexity for LeetCode 3394 (Check if Grid can be Cut into Sections) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3394 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3394 in Java, C++, or Python.

Back to LeetCode Solutions