本文介绍: 最大城市最小电量(二分+前缀和+差分数组+贪心) 礼盒的最大甜蜜度(二分

打卡记录

在这里插入图片描述

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

链接

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

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

链接

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) &gt;&gt; 1
            if check(mid):
                l = mid
            else:
                r = mid - 1
        return l

原文地址:https://blog.csdn.net/qq947467490/article/details/134756002

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_49398.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注