FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/223_Rectangle_Area.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
223_Rectangle_Area.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (23 loc) · 905 Bytes
Breadcrumbs
leetcode
/
java
/
223_Rectangle_Area.java
Copy path
File metadata and controls
30 lines (23 loc) · 905 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
https://leetcode.com/problems/rectangle-area/
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner.
Calculate left, right, top, bottom edges of overlap. If inverted then no overlap, else subtract
overlap area from sum of rectangles.
Time - O(1)
Space - O(1)
*/
public
class
Solution
{
public
int
computeArea
(
int
A
,
int
B
,
int
C
,
int
D
,
int
E
,
int
F
,
int
G
,
int
H
) {
int
right_edge
=
Math
.
min
(
C
,
G
);
int
left_edge
=
Math
.
max
(
A
,
E
);
int
top_edge
=
Math
.
min
(
D
,
H
);
int
bottom_edge
=
Math
.
max
(
B
,
F
);
int
overlap
=
0
;
if
(
right_edge
>
left_edge
&&
top_edge
>
bottom_edge
)
overlap
= (
right_edge
-
left_edge
) * (
top_edge
-
bottom_edge
);
int
r1
= (
C
-
A
) * (
D
-
B
);
int
r2
= (
G
-
E
) * (
H
-
F
);
return
r1
+
r2
-
overlap
;
}
}
Back
|
FazBrowse Home
|
New Git URL