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
相关推荐
怎么没有名字注册了啊29 分钟前
查找成绩(数组实现)
c++·算法
沐怡旸39 分钟前
【算法】725.分割链表--通俗讲解
算法·面试
布林模型43 分钟前
缠论工具czsc快速使用入门(二)
python·缠论·快速入门·czsc
邂逅you1 小时前
用python操作mysql之pymysql库基本操作
数据库·python·mysql
啊森要自信1 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
合作小小程序员小小店1 小时前
web开发,学院培养计划系统,基于Python,FlaskWeb,Mysql数据库
后端·python·mysql·django·web app
渣渣盟1 小时前
解密NLP:从入门到精通
人工智能·python·nlp
老程序员刘飞1 小时前
注册 区块链节点
python
KAIWEILIUCC1 小时前
Python抽象基类(abc.ABC)介绍
python
独行soc2 小时前
2025年渗透测试面试题总结-106(题目+回答)
网络·python·安全·web安全·adb·渗透测试·安全狮