首页 > 其他 > 详细

【leetcode】供暖器

时间:2020-09-26 18:57:39      阅读:65      评论:0      收藏:0      [点我收藏+]

 

#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;
}

 

【leetcode】供暖器

原文:https://www.cnblogs.com/ganxiang/p/13735734.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!