Day48力扣打卡

打卡记录

最大化城市的最小电量(二分+前缀和+差分数组+贪心)

链接

python 复制代码
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] * n
            sum_d = need = 0
            for i, x in enumerate(stations):
                sum_d += diff[i]
                m = target - x - sum_d
                if m > 0:
                    need += m
                    if need > k:
                        return False
                    sum_d += m
                    if i + r * 2 + 1 < n:
                        diff[i + r * 2 + 1] -= m
            return True

        left = min(stations)
        right = left + k
        while left < right:
            mid = (left + right + 1) // 2
            if check(mid):
                left = mid
            else:
                right = mid - 1
        return left

礼盒的最大甜蜜度(二分)

链接

python 复制代码
class Solution:
    def maximumTastiness(self, price: List[int], k: int) -> int:
        n = len(price)
        price.sort()
        def check(x):
            start, cnt = price[0], 1
            for i in range(1, n):
                if price[i] - start >= x:
                    cnt += 1
                    start = price[i]
            return cnt >= k
        l, r = 0, (price[-1] - price[0]) // (k - 1) + 1
        while l < r:
            mid = (l + r + 1) >> 1
            if check(mid):
                l = mid
            else:
                r = mid - 1
        return l
相关推荐
吴佳浩26 分钟前
GPU 编号进阶:CUDA\_VISIBLE\_DEVICES、多进程与容器化陷阱
人工智能·pytorch·python
Liu628881 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
全栈凯哥1 小时前
18.Python中的导入类完全指南
python
AI科技星1 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘
sunwenjian8861 小时前
Java进阶——IO 流
java·开发语言·python
参.商.1 小时前
【Day41】143. 重排链表
leetcode·golang
条tiao条2 小时前
KMP 算法详解:告别暴力匹配,让字符串匹配 “永不回头”
开发语言·算法
guts3502 小时前
图像篡改数据集下载:COVERAGE、CASIA
python·数据集
干啥啥不行,秃头第一名2 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
森林猿2 小时前
java-modbus-读取-modbus4j
java·网络·python