首页 > 数据库 >[Oracle] LeetCode 1010 Pairs of Songs With Total Durations Divisible by 60

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

时间:2022-08-25 17:11:46浏览次数:84  
标签:Pairs Divisible Durations ans long 60 int mp time

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.

Solution

我们用 \(unordered\_map\) 来统计余数,如果 \((t[i]+t[j])\%60=0\), 那么意味着余数的和为 \(60\). 所以直接利用组合数即可

点击查看代码
class Solution {
private:
    
    unordered_map<int,int> mp;
public:
    int numPairsDivisibleBy60(vector<int>& time) {
        int n = time.size();
        long long ans=0;
        for(int i=0;i<n;i++){
            mp[time[i]%60]++;
        }
        for(int i=0;i<=30;i++){
            if(i==0|| i==30){
                if(mp[i]==2)ans++;
                if(mp[i]>2)ans+=(long long)(mp[i]-1)*mp[i]/2;
            }
            else{
                ans+= (long long)(mp[i]*mp[60-i]);
            }
        }
        return ans;
    }
};

标签:Pairs,Divisible,Durations,ans,long,60,int,mp,time
From: https://www.cnblogs.com/xinyu04/p/16624913.html

相关文章

  • 【pytest】命令行参数-durations统计用例运行时间
    前言:写完一个项目的自动化用例之后,发现有些用例运行较慢,影响整体的用例运行速度,于是领导说找出运行慢的那几个用例优化下。--durations参数可以统计出每个用例运行的时......
  • 题解 CF1575D【Divisible by Twenty-Five】
    值域非常小,其中只有\(4\times10^6\)个数是\(25\)的倍数,因此可以暴力枚举所有位数正确的\(25\)的倍数,然后检查是否合法。检查方法就是枚举每一位,如果是数字就必须一......