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
相关推荐
Envyᥫᩣ几秒前
Python中的自然语言处理:从基础到高级
python·自然语言处理·easyui
Amor风信子1 分钟前
华为OD机试真题---跳房子II
java·数据结构·算法
哪 吒1 分钟前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
我是陈泽4 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
hakesashou4 分钟前
python全栈开发是什么?
python
戊子仲秋18 分钟前
【LeetCode】每日一题 2024_10_2 准时到达的列车最小时速(二分答案)
算法·leetcode·职场和发展
邓校长的编程课堂20 分钟前
助力信息学奥赛-VisuAlgo:提升编程与算法学习的可视化工具
学习·算法
创作小达人23 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
ZPC821033 分钟前
Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)
开发语言·python·matplotlib
镜花照无眠35 分钟前
Python爬虫使用实例-mdrama
开发语言·爬虫·python