在歌曲列表中,第 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 整数。

提示:

1 <= time.length <= 60000

1 <= time[i] <= 500

Java版 时间复杂度O(n)

class Solution {
public int numPairsDivisibleBy60(int[] time) {
int i,total=0,len=time.length;
int[] res = new int[60]; // 整除60余数 初始化默认值0
for(i=0;i<len;i++) {
res[time[i]%60]++;
}
//余数为0 的统计 和余数为30 的单独统计
total += res[0]*(res[0]-1)/2 + res[30]*(res[30]-1)/2;
//余数为 1~59的统计(不包括30)
for(i=1;i<30;i++) {
total += res[i] * res[60-i] ;
}
return total;
}
}

C语言版 时间复杂度O(n*n) 提交超时

int numPairsDivisibleBy60(int* time, int timeSize) {
int i,j,total=0;
for (i = 0; i < timeSize-1; i++) {
for (j = i+1; j < timeSize; j++) {
if((time[i]+time[j])%60 == 0) {
total++;
}
}
}
return total;
}

C语言版 时间复杂度O(n)

int numPairsDivisibleBy60(int* time, int timeSize) {
int i,total=0;
int res[60] = {0}; // 整除60余数 初始化默认值0
for(i=0;i<timeSize;i++) {
res[time[i]%60]++;
}
//余数为0 的统计 和余数为30 的单独统计
total += res[0]*(res[0]-1)/2 + res[30]*(res[30]-1)/2;
//余数为 1~59的统计(不包括30)
for(i=1;i<30;i++) {
total += res[i] * res[60-i] ;
}
return total;
}

运行结果

力扣(LeetCode) 1010. 总持续时间可被 60 整除的歌曲的更多相关文章

  1. Leetcode 1013. 总持续时间可被 60 整除的歌曲

    1013. 总持续时间可被 60 整除的歌曲  显示英文描述 我的提交返回竞赛   用户通过次数450 用户尝试次数595 通过次数456 提交次数1236 题目难度Easy 在歌曲列表中,第 i 首 ...

  2. [Swift]LeetCode1010. 总持续时间可被 60 整除的歌曲 | Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  3. 1013. Pairs of Songs With Total Durations Divisible by 60总持续时间可被 60 整除的歌曲

    网址:https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/submissions/ 参考 ...

  4. LeetCode.1010-歌曲总长度可被60整除的对数

    这是小川的第377次更新,第405篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第239题(顺位题号是1010).在歌曲列表中,第i首歌曲的持续时间为[i]秒. 返回其总 ...

  5. 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数

    最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...

  6. 力扣Leetcode 45. 跳跃游戏 II - 贪心思想

    这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...

  7. 【力扣leetcode】-787. K站中转内最便宜的航班

    题目描述: 有 n 个城市通过一些航班连接.给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 p ...

  8. 力扣Leetcode 面试题56 - I. 数组中数字出现的次数

    面试题56 - I. 数组中数字出现的次数 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 示例 ...

  9. 力扣Leetcode 1518. 换酒问题

    小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒.你购入了 numBottles 瓶酒. 如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的. 请你计算 最多 能喝到多少瓶酒. 示例: ...

随机推荐

  1. scrapy 日志处理

    Scrapy生成的调试信息非常有用,但是通常太啰嗦,你可以在Scrapy项目中的setting.py中设置日志显示等级: LOG_LEVEL = 'ERROR' 日志级别 Scrapy日志有五种等级, ...

  2. Navicat使用ssh连接数据库

    1.主机名或ip地址必须填localhost 2.主机名或ip地址必须填外网ip地址

  3. Net中应用 Redis 扩展类

    GIt地址:https://gitee.com/loogn/stackexchange-redis-typedextensions 1.stackexchange 类调用 using System; ...

  4. python简说(二)list

    一.list # 1.list 列表 数组a = ['A', 'B', 'C', 'D']# 0 1 2# 2.空list# a = []# a = list()# 3.下标 角标 索引# print ...

  5. python简说(一)if,for等

    一.python简说 python可以用于自动化测试.web开发.数据分析.AI python.自动化运维,第三方模块最多的一个语言. 编译型语言 c.c++ 要运行,先要编译,编译成二进制的. 解释 ...

  6. opencv学习之路(11)、图像几何变换

    一.图像缩放 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src=imread("E:// ...

  7. CentOS7的安装以及redis的下载安装和连接redis desktop manager出现的问题

    因为需要在springboot下使用redis,所以打算在linux下使用redis,并且使用redis desktop manage来连接管理,但是一路上出现个种问题现在总结一下. 如何安装Cent ...

  8. Codeforces 789D Weird journey - 欧拉路 - 图论

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...

  9. topcoder srm 330 div1

    problem1 link 直接模拟. import java.util.*; import java.math.*; import static java.lang.Math.*; public c ...

  10. HDU 1848 Fibonacci again and again【博弈SG】

    Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F( ...