#1010
Pairs of Songs With Total Durations Divisible by 60
MediumApproaches
💡
Intuition
Time O(n²)Space O(1)
This approach involves checking every possible pair of songs to see if their total duration is divisible by 60. It's straightforward but inefficient for large lists.
⚙️
Algorithm
4 steps- 1Step 1: Initialize a counter to zero.
- 2Step 2: Use two nested loops to iterate through each pair of songs.
- 3Step 3: For each pair, check if the sum of their durations is divisible by 60. If it is, increment the counter.
- 4Step 4: Return the counter.
solution.py8 lines
1def numPairsDivisibleBy60(time):
2 count = 0
3 n = len(time)
4 for i in range(n):
5 for j in range(i + 1, n):
6 if (time[i] + time[j]) % 60 == 0:
7 count += 1
8 return countℹ
Complexity note: The time complexity is O(n²) because we have two nested loops, each iterating through the list of songs. The space complexity is O(1) since we are using a constant amount of extra space.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.