-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0475-Heaters.cs
48 lines (42 loc) · 1.33 KB
/
0475-Heaters.cs
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
//-----------------------------------------------------------------------------
// Runtime: 152ms
// Memory Usage: 35.3 MB
// Link: https://leetcode.com/submissions/detail/359572928/
//-----------------------------------------------------------------------------
using System;
using System.Linq;
namespace LeetCode
{
public class _0475_Heaters
{
public int FindRadius(int[] houses, int[] heaters)
{
Array.Sort(houses);
Array.Sort(heaters);
var distances = new int[houses.Length];
for (int i = 0; i < houses.Length; i++)
distances[i] = int.MaxValue;
for (int i = 0, h = 0; i < houses.Length && h < heaters.Length;)
{
if (houses[i] <= heaters[h])
{
distances[i] = heaters[h] - houses[i];
i++;
}
else
h++;
}
for (int i = houses.Length - 1, h = heaters.Length - 1; i >= 0 && h >= 0;)
{
if (houses[i] >= heaters[h])
{
distances[i] = Math.Min(distances[i], houses[i] - heaters[h]);
i--;
}
else
h--;
}
return distances.Max();
}
}
}