Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.
Now, 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.
So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.
Note:
- Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
- Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
- 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.
Example 1:
Input: [1,2,3],[2] Output: 1 Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
Example 2:
Input: [1,2,3,4],[1,4] Output: 1 Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.
Approach #1:
class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { sort(houses.begin(), houses.end()); sort(heaters.begin(), heaters.end()); vector<int> res(houses.size(), INT_MAX); for (int i = 0, h = 0; i < houses.size() && h < heaters.size(); ) { if (houses[i] <= heaters[h]) { res[i] = heaters[h] - houses[i]; i++; } else h++; } for (int i = houses.size()-1, h = heaters.size()-1; i >= 0 && h >= 0; ) { if (houses[i] >= heaters[h]) { res[i] = min(res[i], houses[i] - heaters[h]); i--; } else h--; } return *max_element(res.begin(), res.end()); } };
Runtime: 92 ms, faster than 13.76% of C++ online submissions for Heaters.
Analysis:
std::max_element
default (1) |
template <class ForwardIterator> ForwardIterator max_element (ForwardIterator first, ForwardIterator last); |
---|---|
custom (2) |
template <class ForwardIterator, class Compare> ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp); |
Returns an iterator pointing to the element with the largest value in the range [first,last)
.
The comparisons are performed using either operator<
for the first version, or comp for the second; An element is the largest if no other element does not compare less than it. If more than one element fulfills this condition, the iterator returned points to the first of such elements.
The behavior of this function template is equivalent to:
|
|
Parameters
- first, last
- Input iterators to the initial and final positions of the sequence to compare. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. - comp
- Binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool
. The value returned indicates whether the element passed as first argument is considered less than the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return value
An iterator to largest value in the range, or last if the range is empty.