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. cakePHP 分页栏

    <div class="page"><?php if ($total > 1) echo $this->element('page_list', ar ...

  2. spring读取工程外配置文件

    因为生产和开发测试的环境不同,所以有时候需要把properties文件放在包外方便修改配置. spring配置文件如下: <context:property-placeholder locati ...

  3. linux下访问window的共享文件,在命令行实现方法

    1.挂载共享目录 mount -t cifs //192.168.0.1/aa  /tmp/export -o username=text,password=test //192.168.0.1/aa ...

  4. 伪异步IO

    针对传统的BIO编程,当客户端数量一直增加的情况下,可能会导致服务器直接奔溃掉,进而出现了一种伪异步IO的线程方式. 先看一下代码: 看一下server端的代码: 其中使用了自定义的一个线程池Hand ...

  5. @CookieValue使用须知

    ------------------------siwuxie095                             @CookieValue 使用须知         使用 @CookieV ...

  6. angular小技巧随笔

    1. 重新刷新页面 同页面切换状态: $state.go('tab.index', {inviteId:inviteId}); self.location.reload();

  7. 用django实现一个资产管理的系统

    整个页面的效果如下图所示 1.登陆注册页面 2.注册的页面 3.登陆页面 4.所有设备 5.正常设备页面 6.退库设备页面 7.丢失设备页面 8.导出设备的页面,仅仅在所有设备页面才支持导出按钮 9. ...

  8. spring boot 2.0 与FASTDFS进行整合

    只支持在spring-boot 2.0以及以上版本中使用 1.pom.xml 里引入FASTDFS的依赖,toobato与fastdfs作者一起,将fastdfs的功能进行了重构与简化 <!-- ...

  9. LINUX SSH修改默认22/添加端口

    通常ssh远程登录的默认端口是22,但是因为端口22属于高危端口,因此很多时候作为服务器会被关掉,不过这个端口一般是可以更改或者添加的,这样除了22端口,也可以通过别的端口进行访问. 1.首先修改配置 ...

  10. Volley超时重试机制

    基础用法 Volley为开发者提供了可配置的超时重试机制,我们在使用时只需要为我们的Request设置自定义的RetryPolicy即可. 参考设置代码如下: int DEFAULT_TIMEOUT_ ...