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.

Idea 1. Similar to Two Sum LT1, map with modular arithmetic

 class Solution {
public int numPairsDivisibleBy60(int[] time) {
Map<Integer, Integer> record = new HashMap<>();
int count = 0;
for(int val: time) {
val = val%60;
count += record.getOrDefault((60 - val)%60, 0);
record.put(val, record.getOrDefault(val, 0) + 1);
} return count;
}
}

array used as map

 class Solution {
public int numPairsDivisibleBy60(int[] time) {
int[] record = new int[60];
int count = 0;
for(int val: time) {
val = val%60;
count += record[(60 - val)%60];
++record[val];
} return count;
}
}

Idea 1a. count pairs, preprose the array first

 class Solution {
public int numPairsDivisibleBy60(int[] time) {
int[] record = new int[60];
for(int val: time) {
val = val%60;
++record[val];
} int count = 0;
if(record[0] > 0) {
count += record[0] * (record[0]-1)/2;
} if(record[30] > 0) {
count += record[30] * (record[30]-1)/2;
} for(int i = 1; i < 30; ++i) {
count += record[i]*record[60-i];
}
return count;
}
}

Note:

  1. 1 <= time.length <= 60000
  2. 1 <= time[i] <= 500

Pairs of Songs With Total Durations Divisible by 60 LT1010的更多相关文章

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

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

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

  4. 【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)

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

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

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. Weekly Contest 128

    1012. Complement of Base 10 Integer Every non-negative integer N has a binary representation.  For e ...

  8. 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665

    package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...

  9. Codeforces #364 DIV2

      ~A题 A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. CentOS 6.5下Redis安装测试

    NoSQL之Redis - CentOS 6.5安装测试 1.下载redis 可以在线安装或者下载 redis ①在线安装前需要检测是否存在rpm包不存在的话查看yum在线是否存在rpm包不存在的话就 ...

  2. CentOS 6.3开机启动服务及自动联网

    虚拟机设置选择NAT模式,默认情况下,CentOS不是自动连接上网的,要点击右上角有个电脑图标,选择system eth0进行连接, 可以修改开机启动配置只需修改:/etc/sysconfig/net ...

  3. fiddler模拟timeout超时场景

    fiddler模拟网络超时: 用fiddler模拟网络请求超时 最近要测试程序对cgi 请求超时的兼容,所以就需要模拟超时,第一个想到的就是fiddler工具,说一下具体的做法: Rules -> ...

  4. cxf怎样提高webservice性能,及访问速度调优

    性能: 1. 启用FastInfoset(快速信息集)webservice的性能实在是不敢恭维.曾经因为webservice吞吐量上不去,对webservice进行了一些性能方面的优化,采用了Fast ...

  5. 581. Shortest Unsorted Continuous Subarray

      Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...

  6. wordpress smtp发送邮件

    准备工作   进入qq邮箱 点击设置 邮箱设置 账户选项 下拉 找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务  开启 点击开启后发送短信内容 配置邮件客户端 到 ...

  7. 8个纯CSS3制作的动画应用及源码

    对于一个复杂的图形或者动画来说,之前我们的处理方式是图片叠加或者利用CSS+JavaScript的方法,然而随着CSS3标准的不断成熟,我们甚至完全可以利用CSS3来绘制一些图片和制作丰富的动画特效. ...

  8. MyBufferedReader

    /** 需求:自定义一个包含 readLine 方法的 BufferedReader 来模拟一下 BufferedReader */ import java.io.FileReader; import ...

  9. 从尾到头打印链表(python)

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, ...

  10. 恶性肿瘤预测Python程序(逻辑回归)

    from sklearn.linear_model import LinearRegression,SGDRegressor,Ridge,LogisticRegression from sklearn ...