Pairs of songs with total durations divisible by 60

October 31, 2022

math-and-geometry

Problem URL: Pairs of songs with total durations divisible by 60

Calculate the time % 60 then it will be exactly same as two sum problem.

class Solution:
    def numPairsDivisibleBy60(self, time: List[int]) -> int:
        lookup = [0] * 60
        res = 0
        for t in time:
            x = t % 60
            res += lookup[60-x] if x > 0 else lookup[0]
            lookup[x] += 1
        return res

Time complexity: O(n)
Space complexity: O(n)