leetcode刷题笔记-Greedy贪婪算法

本文记录了使用贪心算法解决LeetCode中若干问题的思路,包括1648、1921、1432等题目。通过建立映射、双指针、二分搜索等方法,不断优化解决方案,如从O(MN)到O(MlogM),再到O(N)的时间复杂度。同时讨论了在限制空间复杂度为O(1)的情况下如何实现。
摘要由CSDN通过智能技术生成

1648. Sell Diminishing-Valued Colored Balls

class Solution:
    def maxProfit(self, inventory: List[int], n: int) -> int:
        # n is the number of balls client wants
        # O(nlogn) for the sort part, space O(1)
        #  A       先拿上面2个A , 接着AB可以一起拿
        #  A 
        #  A B
        #  A B
        inventory.sort(reverse=True)
        inventory.append(0)
        res = 0
        k = 1 # number of differnet balls with max size
        # print(inventory)
        for i in range(len(inventory) - 1):
            # print(i)
            if inventory[i] > inventory[i+1]:
                diff = inventory[i] - inventory[i+1]
                # print("k*dff, n", k*diff, n)
                if k * diff < n:
                    res += k * (inventory[i] + inventory[i+1] + 1) * diff // 2 # 首项与末项的和项数除以2
                    n -= k*diff
                    # print("n, res", n, res)
                else:
                    q, r = divmod(n, k)  # 总共k排,每排拿q个 还要额外拿r个
                    res += k*(inventory[i] + (inventory[i] - q+1)) * q // 2
                    res += r*(inventory[i] - q)
                    return res % (10**9+7)

            k += 1  # k 要一直加一

1921. Eliminate Maximum Number of Monsters

class Solution:
    def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
        # h = []
        # for i, d in enumerate(dist):
        #     needT = d // speed[i]
        #     if d % speed[i]:  # round up
        #         needT += 1
        #     heapq.heappush(h, needT)

        # re = usedT = 0
        
        # while h:
        #     needT = heapq.heappop(h)
        #     needT -= usedT
        #     if needT <= 0:
        #         return re
        #     re += 1
        #     usedT += 1
        # return re

            
        for i, d in enumerate(dist):
            needT = d // speed[i]
            if d % speed[i]:  
                needT += 1 # round up
            dist[i] = needT
        dist.sort()
        for i, v in enumerate(dist):
            if i >= v:
                return i
        return len(dist)

1798. Maximum Number of Consecutive Values You Can Make

class Solution:
    def getMaximumConsecutive(self, coins: List[int]) -> int:
        '''
        1113
        0, 1, 11, 111, 31, 322, 3111
        Initialize res = 1 since we count from 0;
        Sort coins;
        For each coin in coins:
        if coin > res, then the consecutive values cannot expand to the right;
        if coin == res, then the consecutive values can be just perfectly expanded, res = coin + res;
        if coin < res, then the consecutive values can be expanded, but not as "efficient" as a
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值