Skip to content

Commit 56e89e8

Browse files
committed
Add solution #850
1 parent 194b0ff commit 56e89e8

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@
657657
847|[Shortest Path Visiting All Nodes](./0847-shortest-path-visiting-all-nodes.js)|Hard|
658658
848|[Shifting Letters](./0848-shifting-letters.js)|Medium|
659659
849|[Maximize Distance to Closest Person](./0849-maximize-distance-to-closest-person.js)|Medium|
660+
850|[Rectangle Area II](./0850-rectangle-area-ii.js)|Hard|
660661
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
661662
868|[Binary Gap](./0868-binary-gap.js)|Easy|
662663
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|

solutions/0850-rectangle-area-ii.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* 850. Rectangle Area II
3+
* https://leetcode.com/problems/rectangle-area-ii/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 2D array of axis-aligned rectangles. Each rectangle[i] = [xi1, yi1, xi2, yi2]
7+
* denotes the ith rectangle where (xi1, yi1) are the coordinates of the bottom-left corner, and
8+
* (xi2, yi2) are the coordinates of the top-right corner.
9+
*
10+
* Calculate the total area covered by all rectangles in the plane. Any area covered by two or more
11+
* rectangles should only be counted once.
12+
*
13+
* Return the total area. Since the answer may be too large, return it modulo 109 + 7.
14+
*/
15+
16+
/**
17+
* @param {number[][]} rectangles
18+
* @return {number}
19+
*/
20+
var rectangleArea = function(rectangles) {
21+
const MOD = 1e9 + 7;
22+
const events = [];
23+
const xCoords = new Set();
24+
const yCoords = new Set();
25+
26+
for (const [x1, y1, x2, y2] of rectangles) {
27+
xCoords.add(x1);
28+
xCoords.add(x2);
29+
yCoords.add(y1);
30+
yCoords.add(y2);
31+
events.push([x1, 1, y1, y2]);
32+
events.push([x2, -1, y1, y2]);
33+
}
34+
35+
const xArray = [...xCoords].sort((a, b) => a - b);
36+
const yArray = [...yCoords].sort((a, b) => a - b);
37+
38+
const xMap = new Map();
39+
const yMap = new Map();
40+
41+
for (let i = 0; i < xArray.length; i++) {
42+
xMap.set(xArray[i], i);
43+
}
44+
45+
for (let i = 0; i < yArray.length; i++) {
46+
yMap.set(yArray[i], i);
47+
}
48+
49+
const grid = Array(xArray.length).fill(0).map(() => Array(yArray.length).fill(0));
50+
51+
for (const [x1, y1, x2, y2] of rectangles) {
52+
for (let x = xMap.get(x1); x < xMap.get(x2); x++) {
53+
for (let y = yMap.get(y1); y < yMap.get(y2); y++) {
54+
grid[x][y] = 1;
55+
}
56+
}
57+
}
58+
59+
let totalArea = 0;
60+
61+
for (let x = 0; x < xArray.length - 1; x++) {
62+
for (let y = 0; y < yArray.length - 1; y++) {
63+
if (grid[x][y]) {
64+
const width = xArray[x + 1] - xArray[x];
65+
const height = yArray[y + 1] - yArray[y];
66+
totalArea = (totalArea + width * height) % MOD;
67+
}
68+
}
69+
}
70+
71+
return totalArea;
72+
};

0 commit comments

Comments
 (0)