打卡记录
最大化城市的最小电量(二分+前缀和+差分数组+贪心)
链接
class Solution:def maxPower(self, stations: List[int], r: int, k: int) -> int:n = len(stations)sum = list(accumulate(stations, initial=0))for i in range(n):stations[i] = sum[min(i + r + 1, n)] - sum[max(i - r, 0)]def check(target):diff = [0] * nsum_d = need = 0for i, x in enumerate(stations):sum_d += diff[i]m = target - x - sum_dif m > 0:need += mif need > k:return Falsesum_d += mif i + r * 2 + 1 < n:diff[i + r * 2 + 1] -= mreturn Trueleft = min(stations)right = left + kwhile left < right:mid = (left + right + 1) // 2if check(mid):left = midelse:right = mid - 1return left
礼盒的最大甜蜜度(二分)
链接
class Solution:def maximumTastiness(self, price: List[int], k: int) -> int:n = len(price)price.sort()def check(x):start, cnt = price[0], 1for i in range(1, n):if price[i] - start >= x:cnt += 1start = price[i]return cnt >= kl, r = 0, (price[-1] - price[0]) // (k - 1) + 1while l < r:mid = (l + r + 1) >> 1if check(mid):l = midelse:r = mid - 1return l