[LeetCode] Pairs of Songs With Total Durations Divisible by 60

1010. Pairs of Songs With Total Durations Divisible by 60

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
unordered_map<int, int> m;
for(auto& t : time) {
m[t % 60]++;
}

int res = m[0] * (m[0] - 1) / 2 + m[30] * (m[30] - 1) / 2;

for(int i = 1; i <= 29; i++) {
res += m[i] * m[60 - i];
}

return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/05/PS/LeetCode/pairs-of-songs-with-total-durations-divisible-by-60/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.