Skip to content

Commit 31e4f7d

Browse files
Create heaters.cpp
1 parent fd453da commit 31e4f7d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

heaters.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
4+
int findRadius(vector<int>& houses, vector<int>& heaters) {
5+
sort(houses.begin(), houses.end());
6+
sort(heaters.begin(), heaters.end());
7+
vector<int> heat = heaters;
8+
sort(heat.begin(), heat.end(), greater<int>());
9+
int mini = 0, n = heaters.size();
10+
for (int i = 0; i < houses.size(); i++) {
11+
vector<int> :: iterator it = lower_bound(heaters.begin(), heaters.end(), houses[i]);
12+
vector<int> :: iterator it2 = upper_bound(heat.begin(), heat.end(), houses[i],greater<int>());
13+
int val1, val2;
14+
if (it == heaters.end()) val1 = abs(houses[i] - *heaters.rbegin());
15+
else val1 = abs(houses[i] - *it);
16+
17+
if (it2 == heat.end()) val2 = abs(houses[i] - *heat.rbegin());
18+
else val2 = abs(houses[i] - *it2);
19+
20+
mini = max(mini, min(val1, val2));
21+
}
22+
23+
return mini;
24+
}
25+
};

0 commit comments

Comments
 (0)