In a list of songs, the i
-th 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
.
这个是模的two sum问题
class Solution(object): def numPairsDivisibleBy60(self, time): """ :type time: List[int] :rtype: int """ d = {} ans = 0 for value in time: if (60 - (value % 60)) % 60 in d: ans += d[(60 - (value % 60)) % 60] if (value % 60) in d: d[value % 60] += 1 else: d[value % 60] = 1 return ans