Leetcode-1013 Pairs of Songs With Total Durations Divisible by 60(总持续时间可被 60 整除的歌曲)

class Solution
{
    public:
        int numPairsDivisibleBy60(vector<int>& time)
        {
            int hash[1501] {0};
            for(int i = 0;i < time.size();i ++)
            {
                hash[time[i]] ++;
            }
            
            int rnt = 0;
            for(int i = 1;i < 501;i ++)
            {
                int left = 60-i;
                while(left<0)
                    left += 60;
                while(left>=0&&left <= 1500)
                {
                    if(left==i)
                    {
                        rnt += (hash[i]*(hash[i]-1))/2;
                        left += 60;
                        continue;
                    }
                    rnt += hash[i]*hash[left];
                    left += 60;
                }
                hash[i] = 0;
            }
            return rnt;
        }
};

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/10546541.html