Given n axis-aligned rectangles where n > 0, determine if They all together form an exact cover of a rectangular. Each rectangle are 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 are (1, 1) and Top-right Point is (2, 2 1:
rectangles = [[ 1,1,3,3 ", [ 3,1,4,2 3,2,4,4 1,3,2,4 2,3,3,4 true . All 5 rectangles together form an exact cover of a rectangular region. Example 2:
= [ [1,1,2,3], [1,3,2,4], [3,1,4,2], [3,2,4,4 False3:
= [ [1,1,3,3], [3,1,4,2], [1,3,2,4], [3,2,4,4 false4:
= [ [1,1,3,3], [3,1,4,2], [1,3,2,4], [2,2,4,4 False. Because 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 have 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 to 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 is equal to the sum of small rectangles
1 Public classSolution {2 Public BooleanIsrectanglecover (int[] rectangles) {3 if(rectangles==NULL|| rectangles.length==0 | | rectangles[0].length==0)return false;4 intsubrecareasum = 0;//sum of Subrectangle ' s area5 intx1 = Integer.max_value;//Large Rectangle bottom left x-axis6 inty1 = Integer.max_value;//Large Rectangle bottom left y-axis7 intx2 = integer.min_value;//Large Rectangle top right x-axis8 inty2 = Integer.min_value;//Large Rectangle top right Y-axis9 TenHashset<string> set =NewHashset<string> ();//Store points One A for(int[] rec:rectangles) { - //Check if it has large rectangle ' s 4 points -X1 = math.min (x1, rec[0]); theY1 = math.min (y1, rec[1]); -x2 = Math.max (x2, rec[2]); -y2 = Math.max (y2, rec[3]); - + //calculate sum of Subrectangles -Subrecareasum + = (rec[2]-rec[0]) * (Rec[3]-rec[1]); + A //Store This rectangle ' s 4 points into HashSet atString P1 = integer.tostring (rec[0]) + "" + integer.tostring (rec[1]); -String P2 = integer.tostring (rec[0]) + "" + integer.tostring (rec[3]); -String p3 = integer.tostring (rec[2]) + "" + integer.tostring (rec[1]); -String P4 = integer.tostring (rec[2]) + "" + integer.tostring (rec[3]); - - if(!Set.add (p1)) Set.remove (p1); in if(!Set.add (p2)) Set.remove (p2); - if(!Set.add (p3)) Set.remove (p3); to if(!Set.add (P4)) Set.remove (p4); + } - the if(Set.size ()!=4 | |!set.contains (x1+ "" +y1) | |!set.contains (x1+ "" +y2) | |!set.contains (x2+ "" +y1) | |!set.contains (x2+ "" +y2)) * return false; $ returnSubrecareasum = = (x2-x1) * (y2-y1);Panax Notoginseng } -}
Leetcode:perfect Rectangle