Leetcode: Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region.

Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).

Example 1:
rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [3,2,4,4],
  [1,3,2,4],
  [2,3,3,4]
]

Return true. All 5 rectangles together form an exact cover of a rectangular region.

Example 2:

rectangles = [
  [1,1,2,3],
  [1,3,2,4],
  [3,1,4,2],
  [3,2,4,4]
]

Return false. Because there is a gap between the two rectangular regions.

Example 3:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [3,2,4,4]
]

Return false. Because there is a gap in the top center.

Example 4:


rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [2,2,4,4]
]

Return false. Because two of the rectangles overlap with each other.

Refer to https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java

and   https://discuss.leetcode.com/topic/55923/o-n-solution-by-counting-corners-with-detailed-explaination

Idea

Consider how the corners of all rectangles appear in the large rectangle if there‘s a perfect rectangular cover.
Rule1: The local shape of the corner has to follow one of the three following patterns

    • Corner of the large rectangle (blue): it occurs only once among all rectangles
    • T-junctions (green): it occurs twice among all rectangles
    • Cross (red): it occurs four times among all rectangles

For each point being a corner of any rectangle, it should appear even times except the 4 corners of the large rectangle. So we can put those points into a hash map and remove them if they appear one more time.

At the end, we should only get 4 points.

Rule2:  the large rectangle area should be equal to the sum of small rectangles

 1 public class Solution {
 2     public boolean isRectangleCover(int[][] rectangles) {
 3         if (rectangles==null || rectangles.length==0 || rectangles[0].length==0) return false;
 4         int subrecAreaSum = 0;  //sum of subrectangle‘s area
 5         int x1 = Integer.MAX_VALUE; //large rectangle bottom left x-axis
 6         int y1 = Integer.MAX_VALUE; //large rectangle bottom left y-axis
 7         int x2 = Integer.MIN_VALUE; //large rectangle top right x-axis
 8         int y2 = Integer.MIN_VALUE; //large rectangle top right y-axis
 9
10         HashSet<String> set = new HashSet<String>(); // store points
11
12         for(int[] rec : rectangles) {
13             //check if it has large rectangle‘s 4 points
14             x1 = Math.min(x1, rec[0]);
15             y1 = Math.min(y1, rec[1]);
16             x2 = Math.max(x2, rec[2]);
17             y2 = Math.max(y2, rec[3]);
18
19             //calculate sum of subrectangles
20             subrecAreaSum += (rec[2]-rec[0]) * (rec[3] - rec[1]);
21
22             //store this rectangle‘s 4 points into hashSet
23             String p1 = Integer.toString(rec[0]) + "" + Integer.toString(rec[1]);
24             String p2 = Integer.toString(rec[0]) + "" + Integer.toString(rec[3]);
25             String p3 = Integer.toString(rec[2]) + "" + Integer.toString(rec[1]);
26             String p4 = Integer.toString(rec[2]) + "" + Integer.toString(rec[3]);
27
28             if (!set.add(p1)) set.remove(p1);
29             if (!set.add(p2)) set.remove(p2);
30             if (!set.add(p3)) set.remove(p3);
31             if (!set.add(p4)) set.remove(p4);
32         }
33
34         if (set.size()!=4 || !set.contains(x1+""+y1) || !set.contains(x1+""+y2) || !set.contains(x2+""+y1) || !set.contains(x2+""+y2))
35             return false;
36         return subrecAreaSum == (x2-x1) * (y2-y1);
37     }
38 }
时间: 2024-10-03 20:48:44

Leetcode: Perfect Rectangle的相关文章

LeetCode 850. Rectangle Area II

经典line sweep问题,和 perfect rectangle 很类似,但是要考虑多个矩形左边界一样的情况,加个id来区分. 和另一道类似的题 the skyline problem 不同的是,没有很多 corner cases,对 x 排序非常简单. 难点是在 active set 里,和 merge intervals 那题一样,计算 total_y,乘以两个相邻 event 的 delta_x. class Solution { public: struct interval { i

LeetCode:Rectangle Area - 矩形交叉部分的面积

1.题目名称 Rectangle Area(矩形交叉部分的面积) 2.题目地址 https://leetcode.com/problems/rectangle-area/ 3.题目内容 英文:Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. Given  n  non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above

LeetCode: Maximal Rectangle

LeetCode: Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

LeetCode: Largest Rectangle in Histogram [084]

[题目] Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The lar

LeetCode &quot;Largest Rectangle in Histogram&quot; - TRICKY MONO-QUEUE

I got a DP solution first which is O(n^2). Apparently it is not a optimized one - think about: it is linear value space. There must be O(n) solution. And yes there is: http://fisherlei.blogspot.com/2012/12/leetcode-largest-rectangle-in-histogram.html

[LeetCode] Maximal Rectangle(good)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法: class Solution { public: int maximalRectang

leetcode之Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. 这道