# leetcode 1010. Pairs of Songs With Total Durations Divisible by 60（python）

·  阅读 1450

《望天门山》 ——李白（唐）

### 描述

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.

Example 1:

``````Input: time = [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60

Example 2:

``````Input: time = [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.

Note:

``````1 <= time.length <= 6 * 10^4
1 <= time[i] <= 500

### 解答

``````class Solution(object):
def numPairsDivisibleBy60(self, time):
"""
:type time: List[int]
:rtype: int
"""
for i,t in enumerate(time):
time[i] = t%60
result = 0
for i,t in enumerate(time):
for j in range(i+1,len(time)):
if time[i] + time[j] == 60 or time[i] + time[j] == 0:
result += 1
return result

### 运行结果

``````Time Limit Exceeded

### 解析

• 然后遍历字典即可，如果时间 t == 30 或者 t == 0 的歌曲，直接进行计算 d[t] * (d[t] - 1) // 2 加入到 result 中即可，如遍历到上面取模为 30 的歌曲有 2 首，可以合成 1 对，取模为 0 的歌曲同理计算；

• 否则如果 60 - t 在字典中，d[t] * d[60 - t] 加入到 result 中即可 ，同时为了避免重复计算，将 d[t] 和 d[60-t] 都设置为 0 ，如遍历到取模为 20 的歌曲，肯定是可以和取模为 40 的歌曲结合的，总个数为 d[20] * d[40] , 但是此时已经将两个歌曲的可结合对都计算入结果中，所以将值都置为 0 ，避免后面遍历到取模为 40 的歌曲时，重复加入歌曲对；

• 遍历字典结束，将结果 result 返回。

### 解答

``````class Solution(object):
def numPairsDivisibleBy60(self, time):
"""
:type time: List[int]
:rtype: int
"""
d = {}
result = 0
for i, t in enumerate(time):
t = t % 60
if t not in d:
d[t] = 1
else:
d[t] += 1
time[i] = t
for i, t in enumerate(d):
if t == 30 or t == 0:
c = d[t]
result += c * (c - 1) // 2
elif 60-t in d:
result += (d[t] * d[60 - t])
d[t] = 0
d[60 - t] = 0
return result

### 运行结果

``````Runtime: 192 ms, faster than 73.42% of Python online submissions for Pairs of Songs With Total Durations Divisible by 60.
Memory Usage: 16.2 MB, less than 81.01% of Python online submissions for Pairs of Songs With Total Durations Divisible by 60.