FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/475_Heaters.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
/
475_Heaters.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (32 loc) · 1.51 KB
Breadcrumbs
leetcode
/
java
/
475_Heaters.java
Copy path
File metadata and controls
37 lines (32 loc) · 1.51 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
/*
https://leetcode.com/problems/heaters/
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm
all the houses. You are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters
so that all houses could be covered by those heaters.
Your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius
standard of heaters.
As long as a house is in the heaters' warm radius range, it can be warmed.
All the heaters follow your radius standard and the warm radius will the same.
Sort the houses and heaters in ascending order. For each house, if the next heater is the same distance or closer,
increment the current heater. Then the best closest heater is found, update the minRadius.
Time - O(mlogm + nlogn)
Space - O(m + n)
*/
public
class
Solution
{
public
int
findRadius
(
int
[]
houses
,
int
[]
heaters
) {
Arrays
.
sort
(
houses
);
Arrays
.
sort
(
heaters
);
int
minRadius
=
0
;
int
j
=
0
;
// heater index
int
distance
=
0
;
// between current house and current heater
for
(
int
house
:
houses
) {
distance
=
Math
.
abs
(
house
-
heaters
[
j
]);
while
(
j
<
heaters
.
length
-
1
&&
Math
.
abs
(
house
-
heaters
[
j
+
1
]) <=
distance
) {
distance
=
Math
.
abs
(
house
-
heaters
[
j
+
1
]);
++
j
;
}
minRadius
=
Math
.
max
(
minRadius
,
distance
);
}
return
minRadius
;
}
}
Back
|
FazBrowse Home
|
New Git URL