文章目录
- [6055. 转化时间需要的最少操作数](https://leetcode-cn.com/problems/minimum-number-of-operations-to-convert-time/)
- [5235. 找出输掉零场或一场比赛的玩家](https://leetcode-cn.com/problems/find-players-with-zero-or-one-losses/)
- [5219. 每个小孩最多能分到多少糖果](https://leetcode-cn.com/problems/maximum-candies-allocated-to-k-children/)
- [5302. 加密解密字符串](https://leetcode-cn.com/problems/encrypt-and-decrypt-strings/)
- 总结
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行完成周赛】的目标!