forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.cpp
More file actions
24 lines (24 loc) · 629 Bytes
/
Copy paths3.cpp
File metadata and controls
24 lines (24 loc) · 629 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
// OJ: https://leetcode.com/problems/heaters
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end());
int ans = 0;
for (int h : houses) {
int L = 0, R = heaters.size() - 1;
while (L <= R) {
int M = (L + R) / 2;
if (heaters[M] >= h) R = M - 1;
else L = M + 1;
}
int d = INT_MAX;
if (L < heaters.size()) d = min(d, heaters[L] - h);
if (R >= 0) d = min(d, h - heaters[R]);
ans = max(ans, d);
}
return ans;
}
};