示例 1:
示例 2:
示例 3:
class Solution {public int findRadius(int[] houses, int[] heaters) {Arrays.sort(houses);Arrays.sort(heaters);int res = 0;for(int i = 0,j = 0;iint curDis = Math.abs(houses[i] - heaters[j]);while(j < heaters.length - 1 && Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1])){j++;curDis = Math.min(curDis,Math.abs(houses[i] - heaters[j]));}res = Math.max(curDis,res);}return res;}
}
int compare(const void *a,const void *b)
{int *pa = (int*)a;int *pb = (int*)b;return *pa - *pb;
}int findRadius(int* houses, int housesSize, int* heaters, int heatersSize)
{qsort(houses,housesSize,sizeof(int),compare);qsort(heaters,heatersSize,sizeof(int),compare);int res = 0;for(int i = 0,j=0;iint curDis = fabs(houses[i] - heaters[j]);while(j < heatersSize - 1 && fabs(houses[i] - heaters[j]) >= fabs(houses[i] - heaters[j + 1])){j++;curDis = fabs(houses[i] - heaters[j]);}res = fmax(res,curDis);}return res;
}
class Solution:def findRadius(self, houses: List[int], heaters: List[int]) -> int:houses.sort()heaters.sort()lenHouses = len(houses)lenHeaters = len(heaters)res = 0j = 0for i in range(0,lenHouses):curDis = abs(houses[i] - heaters[j])while j < lenHeaters - 1 and abs(houses[i] - heaters[j]) >= abs(houses[i] - heaters[j + 1]):j+=1curDis = abs(houses[i] - heaters[j])res = max(res,curDis)return res
class Solution {
public:int findRadius(vector& houses, vector& heaters) {sort(houses.begin(),houses.end());sort(heaters.begin(),heaters.end());int res = 0;for(int i = 0,j=0;iint curDis = abs(houses[i] - heaters[j]);while(j < heaters.size() - 1 && abs(houses[i] - heaters[j]) >= abs(houses[i] - heaters[j + 1])){j++;curDis = abs(houses[i] - heaters[j]);}res = max(res,curDis);}return res;}
};
Java语言版
C语言版
Python语言版
C++语言版