Skip to content

Commit 227b0bd

Browse files
committed
Add solution #223
1 parent bd10d49 commit 227b0bd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
220|[Contains Duplicate III](./0220-contains-duplicate-iii.js)|Hard|
205205
221|[Maximal Square](./0221-maximal-square.js)|Medium|
206206
222|[Count Complete Tree Nodes](./0222-count-complete-tree-nodes.js)|Easy|
207+
223|[Rectangle Area](./0223-rectangle-area.js)|Medium|
207208
224|[Basic Calculator](./0224-basic-calculator.js)|Hard|
208209
225|[Implement Stack using Queues](./0225-implement-stack-using-queues.js)|Easy|
209210
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|

solutions/0223-rectangle-area.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 223. Rectangle Area
3+
* https://leetcode.com/problems/rectangle-area/
4+
* Difficulty: Medium
5+
*
6+
* Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area
7+
* covered by the two rectangles.
8+
*
9+
* The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner
10+
* (ax2, ay2).
11+
*
12+
* The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner
13+
* (bx2, by2).
14+
*/
15+
16+
/**
17+
* @param {number} ax1
18+
* @param {number} ay1
19+
* @param {number} ax2
20+
* @param {number} ay2
21+
* @param {number} bx1
22+
* @param {number} by1
23+
* @param {number} bx2
24+
* @param {number} by2
25+
* @return {number}
26+
*/
27+
var computeArea = function(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
28+
return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1)
29+
- Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1))
30+
* Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
31+
};

0 commit comments

Comments
 (0)