You are given a list of songs where the
ith
song has a duration oftime[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 thati < j
with(time[i] + time[j]) % 60 == 0
.
class Solution:
def numPairsDivisibleBy60(self, time):
# len(mp) == 60 代表60分钟
mp = [0] * 60
res = 0
for t in time:
# 读取有几个相同余数的数,能和当前数字组成60
# (当前余数为52,读取到三个余数为8的数能组合)
res += mp[-t % 60]
# 余数记录在列表相应的 index 下
# (余数8 记录在 mp[8])
mp[t % 60] += 1
return res