作者: 负雪明烛
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. 1 <= time.length <= 60000
  2. 1 <= 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)的更多相关文章

  1. 【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 ...

  2. 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/ 参考 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 【LeetCode】面试题62. 圆圈中最后剩下的数字 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 约瑟夫环 日期 题目地址:https://leetco ...

  7. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  8. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. Redis总结笔记

    Redis总结笔记 应用场景 缓存--热数据 计算器 队列 位操作 分布式锁与单线程机制 最新列表 排行榜   Maxmemory-policy算法 volatile-lru:使用LRU算法移除key ...

  2. seqtk抽取测序数据

    做数据比较的时候,由于同一个样本测序数据量不一致,需要抽取数据,控制数据量基本一致. 自己写脚本速度较慢,后面发现一个不错的工具:seqtk 原始数据抽取 如果只控制原始数据量一致,过滤低质量数据后直 ...

  3. java中的Arrays类

    今天刚接触了数组,学到了几个比较常用的方法 Fill方法:给数组赋值 sort方法:给数组升序 equals方法:比较数组中元素 值是否相等 binarySearch方法:对排序好的数组进行二分查找法 ...

  4. ASP.NET Core中使用固定窗口限流

    算法原理 固定窗口算法又称计数器算法,是一种简单的限流算法.在单位时间内设定一个阈值和一个计数值,每收到一个请求则计数值加一,如果计数值超过阈值则触发限流,如果达不到则请求正常处理,进入下一个单位时间 ...

  5. Hadoop RPC通信

    Remote Procedure Call(简称RPC):远程过程调用协议 1. 通过网络从远程计算机程序上请求服务 2. 不需要了解底层网络技术的协议(假定某些传输协议的存在,如TCP或UDP) 3 ...

  6. Shell中单引号和双引号的区别

    1.创建一个test.sh文件 vim test.sh 在文件中添加如下内容 #!/bin/bash do_date=$1 echo "$do_date" echo '$do_da ...

  7. Identity Server 4 从入门到落地(八)—— .Net Framework 客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  8. 容器之分类与各种测试(三)——forward_list的用法

    forward_list是C++11规定的新标准单项链表,slist是g++以前的规定的单项链表 例程 #include<stdexcept> #include<string> ...

  9. Gradle插件详解

    参考[1]Gradle 插件       [2]修改 Gradle 插件(Plugins)的下载地址(repositories)

  10. go 代理

    环境变量中设置 #GO111MODULE=auto GOPROXY=https://goproxy.io 如果不第一次,则在命令行设置 go env -w GO111MODULE=on go env ...