力扣(LeetCode) 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 整数。
提示:
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 整除的歌曲的更多相关文章
- Leetcode 1013. 总持续时间可被 60 整除的歌曲
1013. 总持续时间可被 60 整除的歌曲 显示英文描述 我的提交返回竞赛 用户通过次数450 用户尝试次数595 通过次数456 提交次数1236 题目难度Easy 在歌曲列表中,第 i 首 ...
- [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 ...
- 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/ 参考 ...
- LeetCode.1010-歌曲总长度可被60整除的对数
这是小川的第377次更新,第405篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第239题(顺位题号是1010).在歌曲列表中,第i首歌曲的持续时间为[i]秒. 返回其总 ...
- 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数
最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- 【力扣leetcode】-787. K站中转内最便宜的航班
题目描述: 有 n 个城市通过一些航班连接.给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 p ...
- 力扣Leetcode 面试题56 - I. 数组中数字出现的次数
面试题56 - I. 数组中数字出现的次数 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 示例 ...
- 力扣Leetcode 1518. 换酒问题
小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒.你购入了 numBottles 瓶酒. 如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的. 请你计算 最多 能喝到多少瓶酒. 示例: ...
随机推荐
- OpenGL: 实现立体显示
https://blog.csdn.net/augusdi/article/details/19922295 立体显示原理:设没有立体显示的模型视图矩阵ModelView为Mv,投影矩阵为Mp,则.物 ...
- Caused by: java.sql.SQLException: Field 'category_id' doesn't have a default value
### The error may involve com.qingmu.core.dao.ad.ContentDao.insertSelective-Inline ### The error occ ...
- Baidu WebFE(FEX)团队开发 的 文件上传插件 WebUploader
1.webUploader官网下载地址:http://fex.baidu.com/webuploader/ 直接下载代码,运行examples目录文件即可 2.webUploader上传demo:ht ...
- tcpdump 抓包工具使用
1. 常用命令 监听p4p1网卡上来自 192.168.162.14 的包 tcpdump -i p4p1 src host 192.168.162.14 tcpdump -i p4p1 dst po ...
- gdb远程debug A syntax error in expression, near `variable)'.
今天调试有个linux环境的应用时,gdb提示A syntax error in expression, near `variable)'.,最后经查,gdb版本过低(比如7.2)或者源代码不匹配所致 ...
- 利用shell脚本通过ssh绕过输入密码直接登录主机
shell #!/usr/bin/expect spawn ssh root@192.168.137.141 expect "*password:" send "lizh ...
- 原生js封装的获取某一天是当年的第几周方法
function getWeek(str){ //str格式为yyy-mm-dd //周日归到了本周 var d=new Date(str); var day=d.getDay(); var orig ...
- Plantuml画图工具
1,Plantuml画图工具 安装指南: Mac sublimetext http://blog.csdn.net/zhangjikuan/article/details/53365730 win i ...
- Jordan 块的几何
设 $V$ 是复数域 $\mathbb{C}$ 上的 $n$ 维线性空间, $\varphi$ 是 $V$ 上的线性变换, $A\in M_n(\mathbb{C})$ 是 $\varphi$ 在某组 ...
- linux如何管理物理内存?
Linux kernel version: 5.0.1 arm64 1.将物理内存划分为若干页,每页的大小为4KiB(可以为8KiB或16KiB),那么如何知道每个页当前是什么情况呢? 那就需要一个结 ...