2019-12-30-LeetCode【1010. 总持续时间可被 60 整除的歌曲】

1010. 总持续时间可被 60 整除的歌曲

在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。

返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望索引的数字  i < j 且有 (time[i] + time[j]) % 60 == 0。

示例 1:

输入:[30,20,150,100,40]
输出:3
解释:这三对的总持续时间可被 60 整数:
(time[0] = 30, time[2] = 150): 总持续时间 180
(time[1] = 20, time[3] = 100): 总持续时间 120
(time[1] = 20, time[4] = 40): 总持续时间 60
示例 2:

输入:[60,60,60]
输出:3
解释:所有三对的总持续时间都是 120,可以被 60 整数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pairs-of-songs-with-total-durations-divisible-by-60
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

提示:

  1. 1 <= time.length <= 60000
  2. 1 <= time[i] <= 500

思路

原数组的每一个元素对60取余,将出现的次数保存在一个60个元素的数组中。对于0和30特殊处理配对的数目为count[0]*(count[0]-1)/2、count[30]*(count[30]-1)/2;对于其他的数1~29,31~59,对数为count[i]*count[60-i];

代码

class Solution {
public:
    // int numPairsDivisibleBy60(vector<int>& time) {
    //     int len=time.size();
    //     int res=0;
    //     for(int i=0;i<len;i++)
    //     {
    //         for(int j=i+1;j<len;j++)
    //         {
    //             if((time[i]+time[j])%60==0)
    //             res++;
    //         }
    //     }
    //     return res;
        
    // }

    int numPairsDivisibleBy60(vector<int>& time) {
        int res=0;
        int count[60]={0}; //下表代表余数 元素代表出现的次数
        int len=time.size();
        for(auto i:time)
            count[i%60]++;
        for(int i=1;i<30;i++)
        {
            res+=count[i]*count[60-i];
        }
        res+=count[0]*(count[0]-1)/2;
        res+=count[30]*(count[30]-1)/2;
        return res;
    }
};

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值