|
| 1 | +packagecom.fishercoder.solutions; |
| 2 | + |
| 3 | +importjava.util.HashSet; |
| 4 | +importjava.util.Set; |
| 5 | + |
| 6 | +/** |
| 7 | + * 391. Perfect Rectangle |
| 8 | + * |
| 9 | + * Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. |
| 10 | + Each rectangle is represented as a bottom-left point and a top-right point. |
| 11 | + 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)). |
| 12 | +
|
| 13 | + Example 1: |
| 14 | +
|
| 15 | + rectangles = [ |
| 16 | + [1,1,3,3], |
| 17 | + [3,1,4,2], |
| 18 | + [3,2,4,4], |
| 19 | + [1,3,2,4], |
| 20 | + [2,3,3,4] |
| 21 | + ] |
| 22 | +
|
| 23 | + Return true. All 5 rectangles together form an exact cover of a rectangular region. |
| 24 | +
|
| 25 | + Example 2: |
| 26 | +
|
| 27 | + rectangles = [ |
| 28 | + [1,1,2,3], |
| 29 | + [1,3,2,4], |
| 30 | + [3,1,4,2], |
| 31 | + [3,2,4,4] |
| 32 | + ] |
| 33 | +
|
| 34 | + Return false. Because there is a gap between the two rectangular regions. |
| 35 | +
|
| 36 | + Example 3: |
| 37 | +
|
| 38 | + rectangles = [ |
| 39 | + [1,1,3,3], |
| 40 | + [3,1,4,2], |
| 41 | + [1,3,2,4], |
| 42 | + [3,2,4,4] |
| 43 | + ] |
| 44 | +
|
| 45 | + Return false. Because there is a gap in the top center. |
| 46 | +
|
| 47 | + Example 4: |
| 48 | +
|
| 49 | + rectangles = [ |
| 50 | + [1,1,3,3], |
| 51 | + [3,1,4,2], |
| 52 | + [1,3,2,4], |
| 53 | + [2,2,4,4] |
| 54 | + ] |
| 55 | +
|
| 56 | + Return false. Because two of the rectangles overlap with each other. |
| 57 | + */ |
| 58 | +publicclass_391 { |
| 59 | +/**reference: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java*/ |
| 60 | +publicbooleanisRectangleCover(int[][]rectangles) { |
| 61 | +if (rectangles.length ==0 ||rectangles[0].length ==0)returnfalse; |
| 62 | + |
| 63 | +intx1 =Integer.MAX_VALUE; |
| 64 | +intx2 =Integer.MIN_VALUE; |
| 65 | +inty1 =Integer.MAX_VALUE; |
| 66 | +inty2 =Integer.MIN_VALUE; |
| 67 | + |
| 68 | +Set<String>set =newHashSet<>(); |
| 69 | +intarea =0; |
| 70 | + |
| 71 | +for (int[]rect :rectangles) { |
| 72 | +x1 =Math.min(rect[0],x1); |
| 73 | +y1 =Math.min(rect[1],y1); |
| 74 | +x2 =Math.max(rect[2],x2); |
| 75 | +y2 =Math.max(rect[3],y2); |
| 76 | + |
| 77 | +area += (rect[2] -rect[0]) * (rect[3] -rect[1]); |
| 78 | + |
| 79 | +Strings1 =rect[0] +" " +rect[1]; |
| 80 | +Strings2 =rect[0] +" " +rect[3]; |
| 81 | +Strings3 =rect[2] +" " +rect[3]; |
| 82 | +Strings4 =rect[2] +" " +rect[1]; |
| 83 | + |
| 84 | +if (!set.add(s1))set.remove(s1); |
| 85 | +if (!set.add(s2))set.remove(s2); |
| 86 | +if (!set.add(s3))set.remove(s3); |
| 87 | +if (!set.add(s4))set.remove(s4); |
| 88 | + } |
| 89 | + |
| 90 | +if (!set.contains(x1 +" " +y1) || !set.contains(x1 +" " +y2) || !set.contains(x2 +" " +y1) || !set.contains(x2 +" " +y2) ||set.size() !=4) { |
| 91 | +returnfalse; |
| 92 | + } |
| 93 | + |
| 94 | +returnarea == (x2 -x1) * (y2 -y1); |
| 95 | + } |
| 96 | +} |