-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path475. Heaters
More file actions
34 lines (30 loc) · 980 Bytes
/
475. Heaters
File metadata and controls
34 lines (30 loc) · 980 Bytes
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
public class Solution {
public int FindRadius(int[] houses, int[] heaters) {
Array.Sort(heaters);
int max =0;
foreach(int house in houses)
{
int raduis = FindMinRadius(house, heaters);
max = Math.Max(max, raduis);
}
return max;
}
private int FindMinRadius(int house, int[] heaters)
{ // finding nearest heater to this house
int left = 0;
int right = heaters.Length -1;
while(left <= right)
{
int mid = (right - left) /2 + left;
if(heaters[mid] == house)
return 0;
else if(heaters[mid] < house)
left = mid +1;
else
right = mid -1;
}
int dist1 = (left >= heaters.Length) ? int.MaxValue : Math.Abs(heaters[left] - house);
int dist2 = (right < 0) ? int.MaxValue : Math.Abs(heaters[right] - house);
return Math.Min(dist1, dist2);
}
}