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 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 <= time.length <= 60000
1 <= time[i] <= 500
Pairs of Songs With Total Durations Divisible by 60 LT1010的更多相关文章
- [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 ...
- 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 ...
- 【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 ...
- 【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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/ 参考 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Weekly Contest 128
1012. Complement of Base 10 Integer Every non-negative integer N has a binary representation. For e ...
- 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665
package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...
- Codeforces #364 DIV2
~A题 A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- 1.5.4、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置自定义Java主目录位置)
配置自定义Java主目录位置 注意: Cloudera强烈建议安装JDK/ usr / java / jdk-version,允许Cloudera Manager自动检测并使用正确的JDK版本.如果在 ...
- Compute Shader
[Compute Shader] 1.Similar to regular shaders, compute shaders are Asset files in your project, with ...
- linux上单网卡配置使用多个IP地址
准备一台红帽系列的linux(例如rhel.red hat.centos.fredora等) 方法/步骤 新建配置文件. 首先说明一下规则: 新建配置文件,配置文件名称为ifcfg-适配器名称:0-2 ...
- asp.net MVC 导出excle(转)
转载网址: http://www.cnblogs.com/imr3/articles/2856109.html 还是放到自己这边比较保险. ExportExcel Code public FileRe ...
- metasploit framework(二):记一次入侵
msfconsole use 其中一个 exploit前台执行注入 后台执行shell 加-j 通过sessions查看后台执行的shell,可以看到这个会话的id号为2 进入会话,sessions ...
- Appium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks
问题: 使用Apppium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks 我的启动配置如下 { ...
- python计算两个数的百分比
a和b是整数,计算a/b的百分比 a=3 b=7 a=float(a) b=float(b) 保留百分比后2位小数 print "%.2f%%" % (a/b*100) '42. ...
- 202. Happy Number (INT)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 封装JedisClient.提供API实现对redis的操作
需要导包,jedis-2.8.1.jar和博主的序列化工具类SerializeUtils package com.demo.redis; import java.util.ArrayList; imp ...
- iOS - 常用本机URL跳转设置
import UIKit class ViewController: UIViewController { override func touchesBegan(_ touches: Set<U ...