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
相关推荐
MZ_ZXD0011 小时前
springboot汽车租赁服务管理系统-计算机毕业设计源码58196
java·c++·spring boot·python·django·flask·php
A 计算机毕业设计-小途2 小时前
大四零基础用Vue+ElementUI一周做完化妆品推荐系统?
java·大数据·hadoop·python·spark·毕业设计·毕设
快去睡觉~3 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧3 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油3 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
念念01075 小时前
数学建模竞赛中评价类相关模型
python·数学建模·因子分析·topsis
云天徽上6 小时前
【数据可视化-94】2025 亚洲杯总决赛数据可视化分析:澳大利亚队 vs 中国队
python·信息可视化·数据挖掘·数据分析·数据可视化·pyecharts
☺����6 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
猿究院--王升6 小时前
jvm三色标记
java·jvm·算法
王者鳜錸6 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化