1. 题目
来源:https://tianchi.aliyun.com/oj/164423301311799378/184808348725744274
2. 解题
- 对60求余后,0, 30的为 C n 2 C_n^2 Cn2,其余的相加等于60的,种类相乘
class Solution {
public:
/**
* @param musics: the musics
* @return: calc the number of pair of music
*/
long long musicPairs(vector<int> &musics) {
// write your code here
vector<long long> len(61, 0);
for(auto m : musics)
len[m%60]++;
long long ans = len[0]*(len[0]-1)/2;
for(int i = 1; i <= 29; ++i)
ans += len[i]*len[60-i];
ans += len[30]*(len[30]-1)/2;
return ans;
}
};
151ms C++
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!