【算法】力扣第 287 场周赛(最短代码)

6055. 转化时间需要的最少操作数

贪心,因为只有60, 15, 5, 1四种情况,直接进行计算,二行解法👇

class Solution:
    def convertTime(self, current: str, correct: str) -> int:
        time2int=lambda time:int(time[:2]) * 60 + int(time[3:])
        return (res:=time2int(correct) - time2int(current))//60 + res%60//15 + res%15//5 + res%5

也可以写成一行👇

class Solution:
    def convertTime(self, current: str, correct: str) -> int:
        return (res:=(time2int:=lambda time:int(time[:2]) * 60 + int(time[3:]))(correct) - time2int(current))//60 + res%60//15 + res%15//5 + res%5

5235. 找出输掉零场或一场比赛的玩家

哈希,六弦爷的二行解法👇,非常简洁

class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        wins, loses = map(Counter, zip(*matches))
        return [sorted([w for w in wins if w not in loses]), sorted([l for l in loses if loses[l] == 1])]

5219. 每个小孩最多能分到多少糖果

带key二分,python3.10新特性,灵妙三大佬一行解法👇

class Solution:
    def maximumCandies(self, candies: List[int], k: int) -> int:
        return bisect_right(range(sum(candies) // k), -k, key=lambda x: -sum(v // (x + 1) for v in candies))

5302. 加密解密字符串

逆向思维,参考了灵茶山大佬的题解,四行解法👇

class Encrypter:
    def __init__(self, keys: List[str], values: List[str], dictionary: List[str]):
        self.mp = dict(zip(keys, values))
        self.cnt = Counter(map(self.encrypt, filter(lambda w: all(c in self.mp for c in w), dictionary)))

    def encrypt(self, word1: str) -> str:
        return ''.join(map(self.mp.__getitem__, word1))

    def decrypt(self, word2: str) -> int:
        return self.cnt.get(word2, 0)

总结

T1+T2+T3+T4共1+2+1+4=8行代码,完成【20行完成周赛】的目标!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可可卷

不要看到我~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值