This is one of those problems where the clean idea matters more than the amount of code. For 1010. Pairs of Songs With Total Durations Divisible by 60, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are combCount, numPairsDivisibleBy60, count.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 52 ms, faster than 25.88% of C++ online submissions for Pairs of Songs With Total Durations Divisible by 60.
02//Memory Usage: 11.9 MB, less than 100.00% of C++ online submissions for Pairs of Songs With Total Durations Divisible by 60.
03
04class Solution {
05public:
06 int combCount(int n){
07 return n*(n-1)/2;
08 }
09
10 int numPairsDivisibleBy60(vector<int>& time) {
11 map<int, int> count;
12 vector<int> key; //keys of count
13
14 for(int e : time){
15 e = e%60;
16 if(count.find(e) != count.end()){
17 count[e]++;
18 }else{
19 count[e] = 1;
20 key.push_back(e);
21 }
22 }
23
24 sort(key.begin(), key.end());
25
26 // for(int k : key){
27 // cout << k << ", " << count[k] << endl;
28 // }
29
30 int ans = 0;
31
32 //0 satisfy the condition itself
33 if(find(key.begin(), key.end(), 0) != key.end()) ans+=combCount(count[0]);
34
35 //for the rest, we need the sum of two elements
36 int s = 0, f = key.size()-1;
37 while(s <= f){
38 int k1 = key[s], k2 = key[f];
39 //0 has already been considered
40 if(k1 == 0){
41 s++;
42 continue;
43 }
44 if((k1 + k2)%60 == 0){
45 if(k1 != k2){
46 // cout << k1 << ", " << k2 << ", " << count[k1] * count[k2] << endl;
47 ans += count[k1] * count[k2];
48 }else{
49 ans += combCount(count[k1]);
50 // cout << k1 << ", " << combCount(count[k1]) << endl;
51 }
52 s++;
53 f--;
54 }else if(k1 + k2 < 60){
55 s++;
56 }else{
57 f--;
58 }
59 }
60
61 return ans;
62 }
63};
64
65//TLE
66/**
67class Solution {
68public:
69 int numPairsDivisibleBy60(vector<int>& time) {
70 int ans = 0;
71 for(int i = 0; i < time.size() - 1; i++){
72 for(int j = i + 1; j < time.size(); j++){
73 if((time[i] + time[j])%60 == 0) ans++;
74 }
75 }
76 return ans;
77 }
78};
79**/
80
81/**
82https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/discuss/256738/JavaC%2B%2BPython-Two-Sum-with-K-60
83**/
84
85//Runtime: 36 ms, faster than 98.34% of C++ online submissions for Pairs of Songs With Total Durations Divisible by 60.
86//Memory Usage: 11.7 MB, less than 100.00% of C++ online submissions for Pairs of Songs With Total Durations Divisible by 60.
87
88/**
89class Solution {
90public:
91 int numPairsDivisibleBy60(vector<int>& time) {
92 int ans = 0;
93 vector<int> count(60);
94
95 for(int t : time){
96 //(60-t%60): in 1 ~ 60
97 //(60-t%60)%60: in 0 ~ 59
98 //every time check that how many elements can form a pair with current element
99 //and we update "count" while iterating "time"
100 //this avoid duplicate count
101 ans += count[(60-t%60)%60];
102 count[t%60]++;
103 }
104 return ans;
105 }
106};
107**/
Cost