Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6cf23a6

Browse files
add 391
1 parent ffe2aee commit 6cf23a6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Your ideas/fixes/algorithms are more than welcome!
212212
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_396.java)| O(n^2) could be optimized to O(n) | O(1) | Easy|
213213
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_393.java)| O(?)|O(?) | Medium| Bit Manipulation
214214
|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_392.java)| O(m*n)|O(1) | Medium| Array, String
215+
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_391.java)| O(n)|O(1) | Hard|
215216
|390|[Elimination Game](https://leetcode.com/problems/elimination-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_390.java)| O(logn)|O(1) | Medium|
216217
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_389.java)| O(n)|O(1) | Easy|
217218
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_388.java)| O(n)|O(d) | Medium| Stack
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.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+
public class _391 {
59+
/**reference: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java*/
60+
public boolean isRectangleCover(int[][] rectangles) {
61+
if (rectangles.length == 0 || rectangles[0].length == 0) return false;
62+
63+
int x1 = Integer.MAX_VALUE;
64+
int x2 = Integer.MIN_VALUE;
65+
int y1 = Integer.MAX_VALUE;
66+
int y2 = Integer.MIN_VALUE;
67+
68+
Set<String> set = new HashSet<>();
69+
int area = 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+
String s1 = rect[0] + " " + rect[1];
80+
String s2 = rect[0] + " " + rect[3];
81+
String s3 = rect[2] + " " + rect[3];
82+
String s4 = 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+
return false;
92+
}
93+
94+
return area == (x2 - x1) * (y2 - y1);
95+
}
96+
}

0 commit comments

Comments
 (0)