题解 | 空调遥控
空调遥控
https://www.nowcoder.com/practice/7cb58d56b30c4280ac07ae4428022e03
如何想到滑动窗口以及滑动窗口边界判断?
每个人都忍受温度都是一个区间。当两个人的温度不能满足的时候, 就是最佳温度相差大于2p,可以想像都尽力往中间走(小的+p, 大的-p),但是还是不满足。当我们知道相差2p,就可能想到滑动窗口进行枚举
from math import inf
n, p = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
# 滑动窗口
i = j = 0
res = 0
for j, x in enumerate(arr):
while arr[j] - arr[i] > 2 * p:
i += 1
res = max(res, j - i + 1)
print(res)
查看25道真题和解析