leetcode 第 87 场双周赛





统计共同度过的日子数

将对应的日期映射成为单个数,判断两者的区间是否存在重叠

class Solution:
    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
        day=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        res=[0]*13
        for i in range(12):
            res[i+1]=res[i]+day[i]
       
        def turn_day(s):
            a=int(s[:2])
            b=int(s[-2:])
            return res[a-1]+b
        a1 = turn_day(arriveAlice)
        l1 = turn_day(leaveAlice)
        a2 = turn_day(arriveBob)
        l2 = turn_day(leaveBob)

        return min(l1,l2)-max(a1,a2)+1 if  min(l1,l2)-max(a1,a2)>=0 else 0


运动员和训练师的最大匹配数

简单的贪心处理,从小到大进行排序后一次判断

class Solution:
    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
        players=sorted(players)
        trainers=sorted(trainers)
        res=0
        i,j=0,0
        while i<len(players) and j<len(trainers):
            if players[i]<=trainers[j]:
                res+=1
                i+=1
                j+=1
            else :
                j+=1
        return res

python 可以直接用装饰器实现记忆化还真挺方便的



按位或最大的最小子数组长度

由于题目求起始位置为 i 的最小子数组,那么可以使用从后往前推的顺序
使用一个字典记录每一位1上出现的最新数字,
最后统计字典中最大的位置标记作为结束点

class Solution:
    def smallestSubarrays(self, nums: List[int]) -> List[int]:
        n = len(nums)
        h = defaultdict(int)
        for i in range(n - 1, -1, -1):
            num = nums[i]
            for j in range(30):
                if num >> j & 1:
                    h[j] = i
           
            cnt = max(h.values()) - i + 1 if h else 1
            nums[i] = cnt
        return nums


完成所有交易的初始最少钱数

虽然说是困难题吧,但感觉更像脑筋急转弯
想着贪心之后用排序做,不过摸了几个都不太行
先用大佬的思路
在这里插入图片描述

class Solution:
    def minimumMoney(self, transactions: List[List[int]]) -> int:
        total_lose = mx = 0
        for cost, cashback in transactions:
            total_lose += max(cost - cashback, 0)
            mx = max(mx, min(cost, cashback))
        return total_lose + mx



总结

上周摆烂摸了,不过这周题目都没很难,也没有多少算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值