1010. Pairs of Songs With Total Durations Divisible by 60*
1010. Pairs of Songs With Total Durations Divisible by 60*
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 <= 60000
1 <= time[i] <= 500
C++ 实现 1
参看 [Java/C++/Python] Two Sum with K = 60, 思路非常详细. 大致如下:
将问题转化为 1. Two Sum*, 目标是对于 time
中的每个元素 t % 60
, 在哈希表中查询是否存在 60 - (t % 60)
. 毕竟我们希望找到 x
使得 (x + t) % 60 == 0
. 然而这里存在一个问题, 由于 t % 60
结果是 0 ~ 59
, 但是 60 - (t % 60)
范围却是 1 ~ 60
. 有两种思路:
- 判断
(60 - (t % 60)) % 60)
在哈希表中存在 - 判断
(600 - t) % 60
是否在哈希表中
class Solution {public:int numPairsDivisibleBy60(vector<int>& time) {unordered_map<int, int> records;int res = 0;for (auto &t : time) {res += records[(600 - t) % 60];records[t % 60] ++;}return res;}
};
C++ 实现 2
class Solution {public:int numPairsDivisibleBy60(vector<int>& time) {unordered_map<int, int> records;int res = 0;for (auto &t : time) {res += records[(60 - t % 60) % 60];records[t % 60] ++;}return res;}
};
1010. Pairs of Songs With Total Durations Divisible by 60*相关推荐
- LeetCode 1010. Pairs of Songs With Total Durations Divisible by 60
原题目:https://leetcode-cn.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/ 思路: 用数组m记录 ...
- 【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题目练习总结(持续更新......)
Leetcode题目练习 数组 1.两数之和 26. 删除排序数组中的重复项 27. 移除元素 35.搜索插入位置 53.最大子序列 66.加一 88.合并两个有序数组 118.杨辉三角 119.杨辉 ...
- 成对的歌曲,其总持续时间可被60整除
Problem statement: 问题陈述: In a list of songs, the i-th song has duration of time[i] seconds. Return t ...
- Codeforces #364 DIV2
~A题 A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 高可用 Prometheus 的常见问题
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 监控系统的历史悠久,是一个很成熟的方向,而 Promet ...
- 1065. 单身狗(25)
1065. 单身狗(25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue "单身狗"是中文对 ...
- 基于数据挖掘的共享单车骑行数据分析与预测
温馨提示:文末有 CSDN 平台官方提供的博主 Wechat / QQ 名片 :) 1. 项目背景 共享单车系统在大城市越来越流行,通过提供价格合理的自行车租赁,让人们可以享受在城市里骑自行车的乐趣, ...
- spark数据挖掘 - 基于 Audioscrobbler 数据集音乐推荐实战
基于 Audioscrobbler 数据集音乐推荐实战 1. 数据集 这个例子将使用 Audioscrobbler 公开的数据集.Audioscrobbler是http://www.last.fm/z ...
最新文章
- list对oracle结果集排序了_文章推荐系统系列之基于 FTRL模型的在线排序
- 查看mysql,apache,php,nginx编译参数
- layui 按钮点击一次后失效_00017-layui 对话框 layer.open 点击保存,按钮失效,保存完后,再恢复...
- KMP子串匹配算法(Knuth–Morris–Pratt algorithm)
- JVM生产环境参数实例及分析
- eureka hostname作用_SpringCloud基础教程(三)-Eureka进阶
- asp.net网页上嵌入Flash显示
- 【MFC系列-第33天】链接控件自绘技术
- android系统自动休眠代码流程,Android P 电源管理(4)待机流程
- python split 正则_Python 正则表达式:split
- F8-Nginx代理缓存负载均衡后端均衡
- mockito 多层调用_连续调用的Mockito迭代器样式存根
- PLC的软件故障与硬件故障
- PDF转WORD乱码怎么办
- iMeta教你绘图 | 世界海拔地图
- Halcon标定系列(1):实现机械手手眼标定项目介绍、9点标定
- JMeter使用实践之造数据
- Netatalk CVE-2018-1160的发现与利用
- Problem K: 三角形数
- java mcu视频_如何利用MCU流畅的播放视频?