[关闭]
@snuffles 2019-04-02T16:29:26.000000Z 字数 2363 阅读 869

L475 供暖器

数组


冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。
现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。
所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。
说明:
给出的房屋和供暖器的数目是非负数且不会超过 25000。
给出的房屋和供暖器的位置均是非负数且不会超过10^9。
只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
所有供暖器都遵循你的半径标准,加热的半径也一样。

解:
关键是找到House两侧的heaters,可以顺序找,也可以二分找。

  1. class Solution {
  2. public:
  3. int findRadius(vector<int>& houses, vector<int>& heaters) {
  4. int res=0;
  5. //sort two array
  6. //sort(houses.begin(),houses.end());
  7. sort(heaters.begin(),heaters.end());
  8. for(int house:houses){
  9. auto pos = lower_bound(heaters.begin(),heaters.end(),house);
  10. int dist1 = (pos==heaters.end())?INT_MAX: *pos - house;
  11. int dist2 = (pos==heaters.begin())?INT_MAX:house - *(--pos);
  12. res = max(res,min(dist1,dist2));
  13. }
  14. return res;
  15. }
  16. // 用lower_bound来代替二分查找
  17. int findRadius(vector<int>& houses, vector<int>& heaters) {
  18. int n=heaters.size(),j=0,res=0;
  19. //sort two array
  20. sort(houses.begin(),houses.end());
  21. sort(heaters.begin(),heaters.end());
  22. for(int i=0;i<houses.size();i++){
  23. int left=0,right=n;
  24. while(left<right){
  25. int mid = left + (right-left)/2;
  26. if(heaters[mid]<houses[i]) left=mid+1;
  27. else right=mid;
  28. }
  29. int dist1 = (right==n)?INT_MAX:heaters[right]-houses[i];
  30. int dist2 = (right==0)?INT_MAX:houses[i]-heaters[right-1];
  31. res = max(res,min(dist1,dist2));
  32. }
  33. return res;
  34. }
  35. //再用二分查找缩短时间
  36. int findRadius(vector<int>& houses, vector<int>& heaters) {
  37. int n=heaters.size(),j=0,res=0;
  38. //sort two array
  39. sort(houses.begin(),houses.end());
  40. sort(heaters.begin(),heaters.end());
  41. for(int i=0;i<houses.size();i++){
  42. int cur = houses[i];
  43. // find close heater,left house right, if left>=right继续找
  44. while(j<n-1 && abs(heaters[j+1]-cur) <= abs(heaters[j]-cur) )
  45. ++j;
  46. res = max(res, abs(heaters[j]-cur));
  47. }
  48. return res;
  49. }
  50. //-------------------//
  51. ////多余用radius变量,直接用两个abs比较完了,和res选大的就挺好的。
  52. int findRadius(vector<int>& houses, vector<int>& heaters) {
  53. int res=0;
  54. int radius=0;
  55. int left=0,right=0;
  56. if(heaters.size() ==1){
  57. left = abs(houses.front()-heaters[0]);
  58. right = abs(houses.back()-heaters[0]);
  59. res = left>right?left:right;
  60. return res;
  61. }
  62. sort(houses.begin(),houses.end());
  63. sort(heaters.begin(),heaters.end());
  64. if(heaters[0] > houses.back()) return (heaters[0] -houses.front());
  65. if(heaters.back() < houses[0]) return (heaters.back() -houses.back());
  66. heaters.push_back(INT_MAX);
  67. int max;
  68. int maxj=0;
  69. for(int i=0;i<houses.size();i++){
  70. max = INT_MAX;
  71. for(int j=maxj;j<heaters.size();j++){
  72. left =abs(houses[i] -heaters[j]);
  73. if(j==heaters.size()-1) left = right;
  74. else right = abs(heaters[j+1]-houses[i]);
  75. radius = left<right?left:right;
  76. if(radius <= max) {
  77. max = radius;
  78. maxj = j;
  79. }
  80. else j=heaters.size();
  81. }
  82. if(max > res) res=max;
  83. }
  84. return res;
  85. }
  86. };
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注