获取中...

-

Just a minute...

冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。

在加热器的加热半径范围内的每个房屋都可以获得供暖。现在,给出位于一条水平线上的房屋 houses 和供暖器 heaters 的位置,请你找出并返回可以覆盖所有房屋的最小加热半径。

说明:所有供暖器都遵循你的半径标准,加热的半径也一样。

https://leetcode-cn.com/problems/heaters/

示例 1:

输入: houses = [1,2,3], heaters = [2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。

示例 2:

输入: houses = [1,2,3,4], heaters = [1,4]
输出: 1
解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。

示例 3:

输入:houses = [1,5], heaters = [2]
输出:3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 考虑到所有情况的搜索
var findRadius2 = function (houses, heaters) {
// 1. 取暖器和房子排序
houses.sort((a, b) => {
return a - b
});
heaters.sort((a, b) => {
return a - b
});
let max = 0
const minHeater = heaters[0];
const maxHeater = heaters[heaters.length - 1];

houses.forEach((house, houseIndex) => {
let minRadis;
if (heaters.length === 1) {
let min = heaters[0];
minRadis = Math.abs(house - min);
max = Math.max(max, minRadis)
return
}
if (house <= minHeater) {
minRadis = minHeater - house
max = Math.max(max, minRadis)
return
}
if (house >= maxHeater) {
minRadis = house - maxHeater
max = Math.max(max, minRadis)
return
}
else {
// 在两个中间找到最符合的情况
let midIndex = heaters.findIndex((heater, heaterIndex) => {
const beyond = heater <= house;
const below = house <= heaters[heaterIndex + 1];
return beyond && below
});
minRadis = Math.min(house - heaters[midIndex], heaters[midIndex + 1] - house);
}
max = Math.max(max, minRadis)
})
console.log(max);
return max
}

var findRadius3 = function (houses, heaters) {
let ans = 0;
heaters.sort((a, b) => a - b);
for (const house of houses) {
const i = binarySearch(heaters, house);
const j = i + 1;
const leftDistance = i < 0 ? Number.MAX_VALUE : house - heaters[i];
const rightDistance = j >= heaters.length ? Number.MAX_VALUE : heaters[j] - house;
const curDistance = Math.min(leftDistance, rightDistance);
ans = Math.max(ans, curDistance);
}
return ans;
};

const binarySearch = (nums, target) => {
let left = 0, right = nums.length - 1;
if (nums[left] > target) {
return -1;
}
while (left < right) {
const mid = Math.floor((right - left + 1) / 2) + left;
if (nums[mid] > target) {
right = mid - 1;
} else {
left = mid;
}
}
return left;
}


var findRadius4 = function (houses, heaters) {
houses.sort((a, b) => a - b);
heaters.sort((a, b) => a - b);
let ans = 0;
for (let i = 0, j = 0; i < houses.length; i++) {
let curDistance = 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++;
curDistance = Math.min(curDistance, Math.abs(houses[i] - heaters[j]));
}
ans = Math.max(ans, curDistance);
}
return ans;
};

findRadius2([1, 2, 3], [2])
findRadius([1, 5], [2])
findRadius2([1, 2, 3, 4], [1, 4]) //3
findRadius2([1, 5], [10]) //3
findRadius4(
[474833169, 264817709, 998097157, 817129560],
[197493099, 404280278, 893351816, 505795335]
)
Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1])
findRadius2(
[282475249, 622650073, 984943658, 144108930, 470211272, 101027544, 457850878, 458777923],
[823564440, 115438165, 784484492, 74243042, 114807987, 137522503, 441282327, 16531729, 823378840, 143542612])
相关文章
评论
分享
  • 利用微信小程序扫码授权

    微信小程序扫码授权背景想要使用微信扫码登录自己的网址,通过授权快速获取用户的昵称,头像功能由于没有企业认证账号,故只能通过微信小程序实现, 体验地址https://api.nnnnzs.cn/screen-demo.html?env=...

    利用微信小程序扫码授权
  • 强制加载element-dialog

    强制加载element-dialog背景123<el-dialog> <MyComponent /></el-dialog> 自己封装的组件 MyComponent ,放在了el-dialog里...

    强制加载element-dialog
  • leancloud-基础存储操作

    对象安装使用1234npm install leancloud-storage --save# debug模式DEBUG=leancloud* node src/leancloud.js 初始化12345678const AV = ...

    leancloud-基础存储操作
  • leetcode-540-有序数组中的单一元素

    给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次。请你找出并返回只出现一次的那个数。你设计的解决方案必须满足 O(log n) 时间复杂度和 O(1) 空间复杂度。 示例 1:输入: nums = [1...

    leetcode-540-有序数组中的单一元素
  • 1995.统计特殊四元组

    1995. 统计特殊四元组给你一个 下标从 0 开始 的整数数组 nums ,返回满足下述条件的 不同 四元组 (a, b, c, d) 的 数目 :nums[a] + nums[b] + nums[c] == nums[d] ,且a...

    1995.统计特殊四元组
  • leetcode-1078.Bigram 分词

    给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 “first second third” 形式出现的情况,其中 second 紧随 first 出现,third 紧随 second 出现。对于每...

    leetcode-1078.Bigram 分词
  • leetcode-1705.吃苹果的最大数目

    有一棵特殊的苹果树,一连 n 天,每天都可以长出若干个苹果。在第 i 天,树上会长出 apples[i] 个苹果,这些苹果将会在 days[i] 天后(也就是说,第 i + days[i] 天时)腐烂,变得无法食用。也可能有那么几天,...

    leetcode-1705.吃苹果的最大数目
  • leetcode-1154.一年中的第几天

    1154. 一年中的第几天给你一个字符串 date ,按 YYYY-MM-DD 格式表示一个 现行公元纪年法 日期。请你计算并返回该日期是当年的第几天。通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 ...

    leetcode-1154.一年中的第几天
  • leetcode-997.找到小镇的法官

    997. 找到小镇的法官在一个小镇里,按从 1 到 n 为 n 个人进行编号。传言称,这些人中有一个是小镇上的秘密法官。如果小镇的法官真的存在,那么:小镇的法官不相信任何人。每个人(除了小镇法官外)都信任小镇的法官。只有一个人同时满足...

    leetcode-997.找到小镇的法官
  • leetcode-3.无重复字符的最长子串

    3. 无重复字符的最长子串给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。示例1输入: s = “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。 123456789...

    leetcode-3.无重复字符的最长子串