【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.
Example 1:
Input: [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
Example 2:
Input: [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Note:
1 <= time.length <= 600001 <= time[i] <= 500
题目大意
统计有多少两个数,i < j并且(time[i] + time[j]) % 60 == 0。相当于two sum的改进。
解题方法
这个题怎么想?注意看Note给的提示!首先给出的数组长度是60000,那么是复杂度应该是O(N),看到每个元素的大小是1~500,那么很容易想到,是不是可以对time的元素进行统计,然后统计每两个数字的和是不是60的倍数即可。
首先需要统计每个数字出现的次数。
如果两个数字不同的话,并且这两个数字的和是60的倍数,直接把两个数字的个数相乘。
当然第二个例子提醒了我们,可以在同样的数字中选择两个,所以从相同的数字中任意选择两个,公式是 N * (N - 1) / 2。
Python代码如下:
class Solution(object):
def numPairsDivisibleBy60(self, time):
"""
:type time: List[int]
:rtype: int
"""
count = collections.Counter(time)
key = list(count.keys())
N = len(key)
res = 0
for i, t in enumerate(key):
for j in range(i, N):
if (t + key[j]) % 60 == 0:
if i == j:
res += count[t] * (count[t] - 1) / 2
else:
res += count[t] * count[key[j]]
return res
日期
2019 年 3 月 21 日 —— 好久不刷题,重拾有点难
【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)的更多相关文章
- 【leetcode】1013. 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 pair ...
- 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/ 参考 ...
- 128th LeetCode Weekly Contest 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 ...
- [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 ...
- Pairs of Songs With Total Durations Divisible by 60 LT1010
In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...
- 【LeetCode】面试题62. 圆圈中最后剩下的数字 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 约瑟夫环 日期 题目地址:https://leetco ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
随机推荐
- mysql_sql查性能语句
mysql> SHOW PROCESSLIST; +----+--------+----------------------+-------+-------------+--------+--- ...
- mongodb存储的基本使用
Python连接mongodb一般使用pymongo模块 1. pymongo模块的简单使用 ### MongoDB存储 ## 连接MongoDB import pymongo # 建立连接对象,2种 ...
- do{...}while(0)的用法
零.导引第一次见到 do{...}while(0)是在学习libevent的时候,看到里面有很多类似#define TT_URI(want) do { \ char *ret = evhttp_uri ...
- kubernetes整个基础环境的准备
1.三台centos7,用CentOS-7-x86_64-Minimal-1708.iso安装的,记得统一选好时区,这三台会有etcd集群,其中一台做kubernetes服务端(也可以做三台服务端做负 ...
- ARM汇编基础指令
Cortex-A7 常用汇编指令 一.处理器内部数据传输指令 1.mov 将数据从一个寄存器拷贝到另外一个寄存器,或者将一个立即数传递到寄存器里面 MOV R0,R1 @将寄存器 R1 中的数据传递给 ...
- Java交换数组元素
Java 交换数组元素 代码示例 import java.util.Arrays; import java.util.Collections; import java.util.List; impor ...
- mysql explain using filesort
创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...
- swagger文档
关键配置文件 spring boot demo pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- 远程连接mysql库问题
如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL serve ...
- 关于为了一时方便,使用@Scheduled注解定时踩的坑
摘要: 事情是这样的前两周在做项目的时候碰到一个需求---要求每天晚上执行一个任务,公司统一使用的是 xxl-job 写定时任务的,我当时为了方便自己,然后就简单的使用了Spring的那个@Sched ...