leetcode 1010. Pairs of Songs With Total Durations Divisible by 60(一对歌曲时长被60整除)

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.

Example 1:

Input: time = [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60

给出time数组,找出其中有几对数字,使每对数字的和能被60整除

思路:
参考解法
利用余数的性质,一对数字的和能被60整除,那么它们余数的和也能被60整除。所以只要统计余数的和能被60整除的有多少对。

设数组count[60], 里面统计余数(time[i] % 60)能被60整除的个数 ,即每个元素单独被60整除的情况。

一对数字的和能被60整除,余数重复的情况有2种。

  1. 余数都是0
  2. 余数都是30

余数都是0时,歌曲有几对可用count[0] * (count[0] - 1) / 2计算,原理:假设count[0] = a, 用组合数 C a 2 C_a^2 Ca2, 即从a个数中取2个数的组合数。

同理余数都为30时,从余数为30的个数count[30]中,取2个数的组合。
count[30] * (count[30] - 1) / 2

剩下的是余数不重复的,从1到29遍历,这样就不用除2了
count[i] * count[60 - i]

    public int numPairsDivisibleBy60(int[] time) {
        if(time == null) return 0;
        int result = 0;
        int n = time.length;
        int[] count = new int[60];
        for(int i = 0; i < n; i++) {
            count[time[i] % 60] ++;
        }
        
        //余数都是0
        result += count[0] * (count[0] - 1) / 2;
        //余数都是30
        result += count[30] * (count[30] - 1) / 2;
        
        //余数是1~29
        for(int i = 1; i < 30; i++) {
            result += count[i] * count[60 - i];
        }
        
        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值