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. 有两种思路:

  1. 判断 (60 - (t % 60)) % 60) 在哈希表中存在
  2. 判断 (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*相关推荐

  1. 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记录 ...

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

  3. Leetcode题目练习总结(持续更新......)

    Leetcode题目练习 数组 1.两数之和 26. 删除排序数组中的重复项 27. 移除元素 35.搜索插入位置 53.最大子序列 66.加一 88.合并两个有序数组 118.杨辉三角 119.杨辉 ...

  4. 成对的歌曲,其总持续时间可被60整除

    Problem statement: 问题陈述: In a list of songs, the i-th song has duration of time[i] seconds. Return t ...

  5. Codeforces #364 DIV2

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

  6. 高可用 Prometheus 的常见问题

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 监控系统的历史悠久,是一个很成熟的方向,而 Promet ...

  7. 1065. 单身狗(25)

    1065. 单身狗(25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue "单身狗"是中文对 ...

  8. 基于数据挖掘的共享单车骑行数据分析与预测

    温馨提示:文末有 CSDN 平台官方提供的博主 Wechat / QQ 名片 :) 1. 项目背景 共享单车系统在大城市越来越流行,通过提供价格合理的自行车租赁,让人们可以享受在城市里骑自行车的乐趣, ...

  9. spark数据挖掘 - 基于 Audioscrobbler 数据集音乐推荐实战

    基于 Audioscrobbler 数据集音乐推荐实战 1. 数据集 这个例子将使用 Audioscrobbler 公开的数据集.Audioscrobbler是http://www.last.fm/z ...

最新文章

  1. list对oracle结果集排序了_文章推荐系统系列之基于 FTRL模型的在线排序
  2. 查看mysql,apache,php,nginx编译参数
  3. layui 按钮点击一次后失效_00017-layui 对话框 layer.open 点击保存,按钮失效,保存完后,再恢复...
  4. KMP子串匹配算法(Knuth–Morris–Pratt algorithm)
  5. JVM生产环境参数实例及分析
  6. eureka hostname作用_SpringCloud基础教程(三)-Eureka进阶
  7. asp.net网页上嵌入Flash显示
  8. 【MFC系列-第33天】链接控件自绘技术
  9. android系统自动休眠代码流程,Android P 电源管理(4)待机流程
  10. python split 正则_Python 正则表达式:split
  11. F8-Nginx代理缓存负载均衡后端均衡
  12. mockito 多层调用_连续调用的Mockito迭代器样式存根
  13. PLC的软件故障与硬件故障
  14. PDF转WORD乱码怎么办
  15. iMeta教你绘图 | 世界海拔地图
  16. Halcon标定系列(1):实现机械手手眼标定项目介绍、9点标定
  17. JMeter使用实践之造数据
  18. Netatalk CVE-2018-1160的发现与利用
  19. Problem K: 三角形数
  20. java mcu视频_如何利用MCU流畅的播放视频?

热门文章

  1. 人若不知足,永远不幸福
  2. 美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
  3. html图片自动在div里放大,HTML5+CSS3实现图片的放大/缩小
  4. Keil MDK 和 IAR 两款ARM开发工具区别比较
  5. 什么样的面试更有效?
  6. 使用get-pip.py 安装python2 的pip
  7. 网络连接有个感叹号的原因及解决方法
  8. 数据结构之线性表----一文看懂顺序表、单链表、双链表、循环链表
  9. scikit-learn中的PCA
  10. Intel 英特尔CPU带字母