-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path391.java
More file actions
175 lines (141 loc) · 5.61 KB
/
391.java
File metadata and controls
175 lines (141 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
__________________________________________________________________________________________________
sample 19 ms submission
class Solution {
private static class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return 31 * 31 * x + y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point p = (Point) obj;
return this.x == p.x && this.y == p.y;
}
return false;
}
}
// This is from the best submission!
/*
1. The areas should be the same
2. Apart from the 4 points of the final rectangle, every point is overlapped by 2 and only 2 rectangles!
*/
public boolean isRectangleCover(int[][] rectangles) {
if (rectangles.length == 0) {
return true;
}
// At first, get the bottomLeft and upperRight of the final rectangle.
Point bottomLeft = new Point(rectangles[0][0], rectangles[0][1]);
Point upperRight = new Point(rectangles[0][2], rectangles[0][3]);
Set<Point> points = new HashSet<>();
int area = 0;
for (int[] rect : rectangles) {
if (rect[0] <= bottomLeft.x && rect[1] <= bottomLeft.y) {
bottomLeft = new Point(rect[0], rect[1]);
}
if (rect[2] >= upperRight.x && rect[3] >= upperRight.y) {
upperRight = new Point(rect[2], rect[3]);
}
area += getArea(rect);
for (Point point : new Point[]{
new Point(rect[0], rect[1]),
new Point(rect[0], rect[3]),
new Point(rect[2], rect[1]),
new Point(rect[2], rect[3])}) {
if(!points.add(point)) {
points.remove(point);
}
}
}
if(area != getArea(new int[] {bottomLeft.x, bottomLeft.y, upperRight.x, upperRight.y})) {
return false;
}
return points.size() == 4
&& points.contains(bottomLeft)
&& points.contains(upperRight)
&& points.contains(new Point(bottomLeft.x, upperRight.y))
&& points.contains(new Point(upperRight.x, bottomLeft.y));
}
//89 ms, faster than 15.44%
public boolean isRectangleCoverByBuildingOneByOne(int[][] rectangles) {
if (rectangles.length == 0) {
return true; //??
}
// At first, get the bottomLeft and upperRight of the final rectangle.
int[] bottomLeft = new int[]{rectangles[0][0], rectangles[0][1]};
int[] upperRight = new int[]{rectangles[0][2], rectangles[0][3]};
int area = getArea(rectangles[0]);
for (int i = 1; i < rectangles.length; i++) {
if (rectangles[i][0] <= bottomLeft[0] && rectangles[i][1] <= bottomLeft[1]) {
bottomLeft = new int[]{rectangles[i][0], rectangles[i][1]};
}
if (rectangles[i][2] >= upperRight[0] && rectangles[i][3] >= upperRight[1]) {
upperRight = new int[]{rectangles[i][2], rectangles[i][3]};
}
area += getArea(rectangles[i]);
}
// All area should be the same with the final rectangle candidate.
if (area != getArea(new int[]{bottomLeft[0], bottomLeft[1], upperRight[0], upperRight[1]})) {
return false;
}
// Now let's get the final rectangle one by one from left to right, down to up
Arrays.sort(rectangles, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]);
int[] heights = new int[upperRight[0] - bottomLeft[0] + 1];
for (int[] rect : rectangles) {
// Pay attention that x should not be equal to rect[2]
for (int x = rect[0]; x < rect[2]; x++) {
if (heights[x - bottomLeft[0]] != rect[1] - bottomLeft[1]) {
return false;
}
heights[x - bottomLeft[0]] += rect[3] - rect[1];
}
}
return true;
}
private int getArea(int[] rect) {
return (rect[2] - rect[0]) * (rect[3] - rect[1]);
}
}
__________________________________________________________________________________________________
sample 41848 kb submission
class Solution {
public boolean isRectangleCover(int[][] rectangles) {
Arrays.sort(rectangles, (a, b)->{
if(a[1] != b[1]) return a[1]-b[1];
else{
return a[0]-b[0];
}
});
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
for(int i = 0; i < rectangles.length; i++){
int[] rect = rectangles[i];
minX = Math.min(rect[0], minX);
minY = Math.min(rect[1], minY);
maxX = Math.max(rect[2], maxX);
maxY = Math.max(rect[3], maxY);
}
int [] l = new int[maxX-minX];
Arrays.fill(l, minY);
for(int i = 0; i < rectangles.length; i++){
int[] rect = rectangles[i];
if(rect[2] > maxX) return false;
if(rect[0] < minX) return false;
for(int j = rect[0]; j < rect[2]; j++){
if(l[j-minX] != rect[1]) return false;
l[j-minX] = rect[3];
}
}
for(int i : l) if(i != maxY) return false;
return true;
}
}
__________________________________________________________________________________________________