您的位置:首页 > 其它

LeetCode-475. Heaters

2018-03-24 20:46 288 查看

Description

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

1.Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
2.Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
3.As long as a house is in the heaters' warm radius range, it can be warmed.
4.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.


Solution 1(C++)

class Solution {
public:
/*
Example:    h = house,  * = heater  M = INT_MAX

h   h   h   h   h   h   h   h   h    houses
1   2   3   4   5   6   7   8   9    index
*           *       *                heaters

0   2   1   0   1   0   -   -   -    (distance to nearest RHS heater)
0   1   2   0   1   0   1   2   3    (distance to nearest LHS heater)

0   1   1   0   1   0   1   2   3    (res = minimum of above two)

Result is maximum value in res, which is 3.
*/
int findRadius(vector<int>& A, vector<int>& H) {
sort(A.begin(), A.end());
sort(H.begin(), H.end());
vector<int> res(A.size(), INT_MAX);

// For each house, calculate distance to nearest RHS heater
for (int i = 0, h = 0; i < A.size() && h < H.size(); ) {
if (A[i] <= H[h]) { res[i] = H[h] - A[i]; i++; }
else { h++; }
}

// For each house, calculate distance to nearest LHS heater
for (int i = A.size()-1, h = H.size()-1; i >= 0 && h >= 0; ) {
if (A[i] >= H[h]) { res[i] = min(res[i], A[i] - H[h]); i--; }
else { h--; }
}

return *max_element(res.begin(), res.end());
}
};


Solution 2(C++)

class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end());
int minRadius = 0;
for (int i = 0; i < houses.size(); i++) {
// search for the closest heater whose position is at least the current house's position
auto larger = lower_bound(heaters.begin(), heaters.end(), houses[i]);
int curRadius = INT_MAX;
// if there is such a heater, update the radius for that heater to cover this house if necessary
if (larger != heaters.end())
curRadius = *larger - houses[i];
// if the heater we found is not the first one, then the previous heater is the closest heater
// whose position is smaller than the current house's position
if (larger != heaters.begin()) {
auto smaller = larger - 1;
// the heater with the smaller required radius to cover the house wins
curRadius = min(curRadius, houses[i] - *smaller);
}
minRadius = max(minRadius, curRadius);
}
return minRadius;
}
};


算法分析

这道题,光是题目理解就挺费劲的了。

解法一:我真的好像是在哪一道题看到过这样类似的解法,特点就是,一列数从左往右按照某种运算有一种结果,然后从右往左按照某种运算又有一种结果,然后最终结果就是两者进行一个比较。这种方法似曾相识,但是我现在想不太起来了。后面如果想起来,或者又遇到类似的方法,会会来找的。

解法二:解法二其实就是对每一个房子要找到与其最相近的两个加热器,一个比他大,另一个比他小,然后,两个加热器到一个房子会有两个加热距离,为了保证房子被加热,那么这两个距离中最小的距离就是这个房子能被加热到所需的最小距离,然后每个房子都有一个最小加热距离,从所有最小加热距离中,选择一个最大的加热距离,那么就是题目要求的辐射距离了。

关于解法二,第一点,为什么只用考虑每个房子的两个加热器的加热距离中的最小距离?如果选择了最小加热距离,会不会导致后面同一个加热器因为要加热其他房子,而导致加热距离更改而导致该房子无法被加热到?因为首先每个加热器是向两边加热的。然后现在是假想情况,目的就是首先判断出每个房子需要的最小加热距离。然后要保证每个房子都能被加热到,所有的房子的加热距离中选择最大的那个,那么就能保证每个房子都被加热到了。

解法二的故事告诉我们,做题时,我被房子与加热器晃晕了,这时候多个条件干扰的时候,不如抓紧一个。将问题简单抽象。与其一次性考虑那么多房子那么多加热器,还不如简单考虑一个房子,或者考虑一个加热器。都是可以的。

这就是化繁为简的能力。

程序分析

解法二中用到了函数lower_bound(),这个函数其实是algorithm中的一个函数,就是利用二分法查找要求的。可参考:std::lower_bound

简单来说,函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: