#define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b)) int cmp(const void* a,const void* b){ return *(int*)a > *(int*)b; } int findRadius(int* houses, int housesSize, int* heaters, int heatersSize){ int i=0,j=0,radius=1000000001,min,minRadius=0; qsort(houses,housesSize,sizeof(int),cmp); qsort(heaters,heatersSize,sizeof(int),cmp); while(i<housesSize && j<heatersSize) { min = (j>0)? MIN(abs(heaters[j]-houses[i]),abs(heaters[j-1]-houses[i])): abs(heaters[j]-houses[i]); if (min < radius) radius=min; if (houses[i] <= heaters[j]) { if (houses[i]==heaters[j]) j++; if (min > minRadius) minRadius=min; i++; radius=1000000001; } else j++; } if (j==heatersSize && i!=housesSize && houses[housesSize-1] - heaters[j-1] > minRadius) minRadius=houses[housesSize-1] - heaters[j-1]; return minRadius; }
原文:https://www.cnblogs.com/ganxiang/p/13735734.html