返回介绍

solution / 0400-0499 / 0475.Heaters / README_EN

发布于 2024-06-17 01:04:00 字数 5340 浏览 0 评论 0 收藏 0

475. Heaters

中文文档

Description

Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.

Every house can be warmed, as long as the house is within the heater's warm radius range. 

Given the positions of houses and heaters on a horizontal line, return _the minimum radius standard of heaters so that those heaters could cover all houses._

Notice that all the heaters follow your radius standard, and the warm radius will the same.

 

Example 1:

Input: houses = [1,2,3], heaters = [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: houses = [1,2,3,4], heaters = [1,4]
Output: 1
Explanation: The two heaters were placed at positions 1 and 4. We need to use a radius 1 standard, then all the houses can be warmed.

Example 3:

Input: houses = [1,5], heaters = [2]
Output: 3

 

Constraints:

  • 1 <= houses.length, heaters.length <= 3 * 104
  • 1 <= houses[i], heaters[i] <= 109

Solutions

Solution 1

class Solution:
  def findRadius(self, houses: List[int], heaters: List[int]) -> int:
    houses.sort()
    heaters.sort()

    def check(r):
      m, n = len(houses), len(heaters)
      i = j = 0
      while i < m:
        if j >= n:
          return False
        mi = heaters[j] - r
        mx = heaters[j] + r
        if houses[i] < mi:
          return False
        if houses[i] > mx:
          j += 1
        else:
          i += 1
      return True

    left, right = 0, int(1e9)
    while left < right:
      mid = (left + right) >> 1
      if check(mid):
        right = mid
      else:
        left = mid + 1
    return left
class Solution {
  public int findRadius(int[] houses, int[] heaters) {
    Arrays.sort(heaters);
    int res = 0;
    for (int x : houses) {
      int i = Arrays.binarySearch(heaters, x);
      if (i < 0) {
        i = ~i;
      }
      int dis1 = i > 0 ? x - heaters[i - 1] : Integer.MAX_VALUE;
      int dis2 = i < heaters.length ? heaters[i] - x : Integer.MAX_VALUE;
      res = Math.max(res, Math.min(dis1, dis2));
    }
    return res;
  }
}
class Solution {
public:
  int findRadius(vector<int>& houses, vector<int>& heaters) {
    sort(houses.begin(), houses.end());
    sort(heaters.begin(), heaters.end());
    int left = 0, right = 1e9;
    while (left < right) {
      int mid = left + right >> 1;
      if (check(houses, heaters, mid))
        right = mid;
      else
        left = mid + 1;
    }
    return left;
  }

  bool check(vector<int>& houses, vector<int>& heaters, int r) {
    int m = houses.size(), n = heaters.size();
    int i = 0, j = 0;
    while (i < m) {
      if (j >= n) return false;
      int mi = heaters[j] - r;
      int mx = heaters[j] + r;
      if (houses[i] < mi) return false;
      if (houses[i] > mx)
        ++j;
      else
        ++i;
    }
    return true;
  }
};
func findRadius(houses []int, heaters []int) int {
  sort.Ints(houses)
  sort.Ints(heaters)
  m, n := len(houses), len(heaters)

  check := func(r int) bool {
    var i, j int
    for i < m {
      if j >= n {
        return false
      }
      mi, mx := heaters[j]-r, heaters[j]+r
      if houses[i] < mi {
        return false
      }
      if houses[i] > mx {
        j++
      } else {
        i++
      }
    }
    return true
  }
  left, right := 0, int(1e9)
  for left < right {
    mid := (left + right) >> 1
    if check(mid) {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return left
}
function findRadius(houses: number[], heaters: number[]): number {
  houses.sort((a, b) => a - b);
  heaters.sort((a, b) => a - b);
  const m = houses.length,
    n = heaters.length;
  let ans = 0;
  for (let i = 0, j = 0; i < m; i++) {
    let cur = Math.abs(houses[i] - heaters[j]);
    while (
      j + 1 < n &&
      Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1])
    ) {
      cur = Math.min(Math.abs(houses[i] - heaters[++j]), cur);
    }
    ans = Math.max(cur, ans);
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文