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.
Constraints:
1 <= time.length <= 6 * 104
1 <= time[i] <= 500
This problem asks to find the number of pairs of songs from a given list whose total duration is divisible by 60.
This approach leverages a frequency counter (using a hash map or array) to efficiently count the remainders when song durations are divided by 60. We then calculate pairs based on these remainders.
Algorithm:
Calculate Remainders: Iterate through the time
array and calculate the remainder of each song duration when divided by 60. Store these remainders in a frequency counter (cnt
).
Count Pairs:
cnt[x] * cnt[60 - x]
.cnt[0] * (cnt[0] - 1) / 2
(combinations).cnt[30] * (cnt[30] - 1) / 2
.Return Total: Sum up the number of pairs from steps 2.
Time Complexity: O(N), where N is the length of the time
array. We iterate through the array once to calculate remainders and once to count pairs.
Space Complexity: O(1). The frequency counter cnt
has a fixed size of 60.
Code (Python):
from collections import Counter
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
cnt = Counter(t % 60 for t in time)
ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
ans += cnt[0] * (cnt[0] - 1) // 2
ans += cnt[30] * (cnt[30] - 1) // 2
return ans
Code in other languages (Java, C++, Go, TypeScript) follows a similar structure, replacing the Python Counter
with equivalent data structures (arrays or HashMaps).
This approach is slightly more efficient in terms of code and avoids redundant calculations.
Algorithm:
Initialize Counter: Create a frequency counter (cnt
) initialized to all zeros.
Iterate and Count: Iterate through the time
array. For each song duration x
:
x % 60
.y = (60 - x) % 60
.y
(from the counter) to the total count ans
. This represents the number of pairs found so far.x
in the counter.Return Total: Return the final ans
.
Time Complexity: O(N), similar to Approach 1.
Space Complexity: O(1), the space used is constant.
Code (Python):
from collections import Counter
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
cnt = Counter()
ans = 0
for x in time:
x %= 60
y = (60 - x) % 60
ans += cnt[y]
cnt[x] += 1
return ans
Again, code in other languages would adapt the counter to the language's built-in structures. This approach is generally preferred for its conciseness and slight efficiency gain.