题目
You are given a list of songs where the ith song has a duration of time[i] seconds.
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.
解题思路
求出所有时间对60取余的出现的次数,i和60-i配对,0和30要单独处理
代码
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
ct = Counter([t % 60 for t in time])
res = 0
res += ct[0] * (ct[0] - 1) // 2
res += ct[30] * (ct[30] - 1) // 2
res += sum([ct[i] * ct[60 - i] for i in range(1, 30)])
return res