1
class Solution {
2
public int rectangleArea(int[][] rectangles) {
3
int n = rectangles.length;
4
Set<Integer> coorx = new HashSet<>();
5
Set<Integer> coory = new HashSet<>();
6
for (int[] rec : rectangles) {
7
coorx.add(rec[0]);
8
coorx.add(rec[2]);
9
coory.add(rec[1]);
10
coory.add(rec[3]);
11
}
12

13
Integer[] compressx = coorx.toArray(new Integer[0]);
14
Arrays.sort(compressx);
15
Integer[] compressy = coory.toArray(new Integer[0]);
16
Arrays.sort(compressy);
17

18
Map<Integer, Integer> mapx = new HashMap<>();
19
for (int i = 0; i < compressx.length; i++) {
20
mapx.put(compressx[i], i);
21
}
22
Map<Integer, Integer> mapy = new HashMap<>();
23
for (int i = 0; i < compressy.length; i++) {
24
mapy.put(compressy[i], i);
25
}
26

27
boolean[][] grid = new boolean[compressx.length][compressy.length];
28
for (int[] rec : rectangles) {
29
for (int x = mapx.get(rec[0]); x < mapx.get(rec[2]); x++) {
30
for (int y = mapy.get(rec[1]); y < mapy.get(rec[3]); y++) {
31
grid[x][y] = true;
32
}
33
}
34
}
35

36
long res = 0L;
37
for (int i = 0; i < grid.length; i++) {
38
for (int j = 0; j < grid[0].length; j++) {
39
if (grid[i][j]) {
40
res += (long) (compressx[i + 1] - compressx[i]) * (compressy[j + 1] - compressy[j]);
41
}
42
}
43
}
44
res %= 1000000007;
45
return (int) res;
46
}
47
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0