【leetcode】475. Heaters(Python & C++)


475. Heaters

题目链接

475.1 题目描述:

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.

475.2 解题思路:

开始之前都要对house和heater排序,以方便查找。

两种思路都是遍历house数组,找出每个house距离最近的那个heater,它们之间的距离作为此house所需的半径,算出每个house所需的半径,其中最大者即为要找的半径。

思路不同的是在于找距离每个house最近的heater上。这个heater不是与house相等,就是在house左右两边,第一个大于或第一个小于的值。

  1. 思路一:在遍历house时,对每一个house,遍历heater数组,比较house与heater的值,相等的话,则直接为此house的所需半径,小于的话,如记录此时的坐标s,继续往前走,大于的话,停止前进。因为无需再往前找heater了。下次找heater的时候,就可以从s开始遍历了。

  2. 思路二:设置一个index,来表示离house最近的heater的坐标。每次遍历house时,对于每个house,循环heater,条件为,index没有到heater最后一个,且heater[index+1]与house[i]的差小于heater[index]与house[i]的差,这说明,此时的index处的heater离house不是最近,index++。循环heater完成后,返回maxinx和heater[index]与house[i]的差中较大的那一个。

注意: Python中有一个比较坑的地方。就是sorted()函数和sort()两个排序函数不同,虽然sorted 和list.sort 都接受key, reverse定制,但是list.sort()是列表中的方法,只能用于列表。而sorted可以用于任何可迭代的对象。list.sort()是在原序列上进行修改,不会产生新的序列。所以如果你不需要旧的序列,可以选择list.sort()。 sorted() 会返回一个新的序列,旧的对象依然存在。所以当你排序时,如果你用sort(),则原序列直接修改,不必担心。但如果你用了sorted(),一定要赋值于新列表上操作,不然不会发生改变。

475.3 C++代码:

1、思路一代码(76ms):

class Solution121 {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        int maxidx = 0;
        int s = 0;  
        for (int i = 0; i < houses.size();i++)
        {
            int temp = abs(heaters[s]-houses[i]);
            for (int j = s+1; j < heaters.size();j++)
            {
                if (houses[i] == heaters[j])
                {
                    temp = 0;
                    break;
                }
                else if (houses[i] < heaters[j])
                {
                    if (heaters[j] - houses[i]< temp)
                        temp = heaters[j] - houses[i];
                    break;
                }
                else
                {
                    if (houses[i] - heaters[j]if (temp > maxidx)
                maxidx = temp;
        }
        return maxidx;
    }
};

2、思路二代码(86ms)

class Solution121_1 {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        int maxidx = 0;
        int index = 0;
        for (int i = 0; i < houses.size();i++)
        {
            while (index + 1 < heaters.size() && (abs(heaters[index + 1] - houses[i]) <= abs(heaters[index] - houses[i])))
                index++;
            int temp = abs(heaters[index] - houses[i]);
            maxidx = maxidx < temp ? temp : maxidx;
        }
        return maxidx;
    }
};

475.4 Python代码:

1、思路一代码(超时ms)

class Solution(object):
    def findRadius(self, houses, heaters):
        """
        :type houses: List[int]
        :type heaters: List[int]
        :rtype: int
        """
        houses.sort()
        heaters.sort()        
        maxidx=0
        s=0
        for i in range(len(houses)):
            temp=abs(heaters[s]-houses[i])
            for j in range(s+1,len(heaters)):
                if heaters[j]==houses[i]:
                    temp=0
                    break
                elif heaters[j]if houses[i]-heaters[j]else:
                    if heaters[j]-houses[i]break
            if temp>maxidx:
                maxidx=temp
        return maxidx

2、思路二代码(185ms)

class Solution1(object):
    def findRadius(self, houses, heaters):
        """
        :type houses: List[int]
        :type heaters: List[int]
        :rtype: int
        """
        houses.sort()
        heaters.sort()
        maxidx=0
        index=0
        for i in range(len(houses)):
            while index+1and (abs(heaters[index+1]-houses[i])<=abs(heaters[index]-houses[i])):
                index+=1
            maxidx=max(maxidx,abs(heaters[index]-houses[i]))
        return maxidx

你可能感兴趣的:(leetcode,算法,Leetcode)