贪心,因为只有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
哈希,六弦爷的二行解法,非常简洁
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])]
带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))
逆向思维,参考了灵茶山大佬的题解,四行解法
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行完成周赛】的目标!