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
相关推荐
NAGNIP1 天前
万字长文!回归模型最全讲解!
算法·面试
知乎的哥廷根数学学派1 天前
面向可信机械故障诊断的自适应置信度惩罚深度校准算法(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习·矩阵
且去填词1 天前
DeepSeek :基于 Schema 推理与自愈机制的智能 ETL
数据仓库·人工智能·python·语言模型·etl·schema·deepseek
人工干智能1 天前
OpenAI Assistants API 中 client.beta.threads.messages.create方法,兼谈一星*和两星**解包
python·llm
databook1 天前
当条形图遇上极坐标:径向与圆形条形图的视觉革命
python·数据分析·数据可视化
阿部多瑞 ABU1 天前
`chenmo` —— 可编程元叙事引擎 V2.3+
linux·人工智能·python·ai写作
acanab1 天前
VScode python插件
ide·vscode·python
666HZ6661 天前
数据结构2.0 线性表
c语言·数据结构·算法
知乎的哥廷根数学学派1 天前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习
实心儿儿1 天前
Linux —— 基础开发工具5
linux·运维·算法