FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/src/RectangleArea.java at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
src
/
RectangleArea.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
49 lines (41 loc) · 1.82 KB
Breadcrumbs
leetcode-algorithms
/
src
/
RectangleArea.java
Copy path
File metadata and controls
49 lines (41 loc) · 1.82 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// https://leetcode.com/problems/rectangle-area
// T: O(1)
// S: O(1)
public
class
RectangleArea
{
private
record
Point
(
int
x
,
int
y
) {}
private
static
class
Rectangle
{
public
final
int
xl
;
public
final
int
xr
;
public
final
int
yb
;
public
final
int
yt
;
public
final
int
area
;
Rectangle
(
Point
lowerLeft
,
Point
upperRight
) {
xl
=
lowerLeft
.
x
;
xr
=
upperRight
.
x
;
yb
=
lowerLeft
.
y
;
yt
=
upperRight
.
y
;
area
= (
upperRight
.
x
-
lowerLeft
.
x
) * (
upperRight
.
y
-
lowerLeft
.
y
);
}
public
static
Rectangle
from
(
int
x1
,
int
y1
,
int
x2
,
int
y2
) {
final
Point
a1
=
new
Point
(
x1
,
y1
);
final
Point
a2
=
new
Point
(
x2
,
y2
);
return
new
Rectangle
(
a1
,
a2
);
}
public
Rectangle
intersection
(
Rectangle
other
) {
final
int
intersectionXl
=
Math
.
max
(
xl
,
other
.
xl
);
final
int
intersectionXr
=
Math
.
max
(
intersectionXl
,
Math
.
min
(
xr
,
other
.
xr
));
final
int
intersectionYb
=
Math
.
max
(
yb
,
other
.
yb
);
final
int
intersectionYt
=
Math
.
max
(
intersectionYb
,
Math
.
min
(
yt
,
other
.
yt
));
if
(
intersectionXl
>
intersectionXr
||
intersectionYb
>
intersectionYt
) {
return
null
;
}
return
Rectangle
.
from
(
intersectionXl
,
intersectionYb
,
intersectionXr
,
intersectionYt
);
}
}
public
static
int
computeArea
(
int
ax1
,
int
ay1
,
int
ax2
,
int
ay2
,
int
bx1
,
int
by1
,
int
bx2
,
int
by2
) {
final
Rectangle
rectangle1
=
Rectangle
.
from
(
ax1
,
ay1
,
ax2
,
ay2
);
final
Rectangle
rectangle2
=
Rectangle
.
from
(
bx1
,
by1
,
bx2
,
by2
);
final
Rectangle
intersection
=
rectangle1
.
intersection
(
rectangle2
);
return
rectangle1
.
area
+
rectangle2
.
area
- (
intersection
==
null
?
0
:
intersection
.
area
);
}
}
Back
|
FazBrowse Home
|
New Git URL