Problem statement:

问题陈述:

In a list of songs, the i-th song has 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).

在歌曲列表中,第i首歌曲的持续时间为[i]秒。 返回其总持续时间(以秒为单位)可被60整除的歌曲对数 。 形式上,我们希望索引i <j的数量为( time [i] + time [j])%60 == 0 )。

Example:

例:

    Input array:
10, 20, 30, 60, 80, 110, 120
Output:
Number of such pairs:
2
Pairs are:
10, 110
60, 120

Solution:

解:

Of course, there is a naïve solution using brute force technique. It's as simple as checking sum for every possible pair with a time complexity of O(n^2).

当然,存在使用暴力技术的幼稚解决方案。 就像检查每个可能的对的总和一样简单,时间复杂度为O(n ^ 2)

Efficient solution can be done using mathematical concepts of congruent modulo and combinatorics.

可以使用等价模和组合数学概念来完成有效的解决方案。

Let's revise what are the cases for a pair sum divisible by 60

让我们修改一下被60整除的对的情况

  1. Both the numbers of the pair divisible by 60.

    这对数字都可以被60整除。

  2. The sum of their congruent modulo 60 is divisible by 60.

    它们的全模60的和可被60整除。

So actually all the elements of the array can be grouped by congruent modulo.
Since it’s modulo 60.
Maximum remainder can be 59.
Remainders can be any number between 0 to 59.

因此,实际上,数组的所有元素都可以按全模来分组。
由于它是模60。
最大余数可以是59。
余数可以是0到59之间的任何数字。

We actually group all the elements based on modulo value.

实际上,我们根据模值对所有元素进行分组。

  1. Declare group[60]={0}; //since their can be 60 possible remainders starting from 0 to 59

    声明组[60] = {0}; //因为它们可以是0到59之间的60个余数

  2. For I in input array

    对于我在输入数组

    group[i%60]++;

    组[i%60] ++;

In this way our group is formed.
If group[j] is K, that simply means there are K elements in the array for each of them modulo 60 is j

这样,我们的小组就形成了。
如果group [j]为K ,则仅表示数组中有K个元素,每个元素的模60为j

So after grouping,

所以分组之后

    10, 20, 30, 60, 80, 110, 120
group[10]=1 //10
group[20]=2 //20,80
group[30]=1 //30
group[50]=1 //110
group[0]=2 //60,120

Now we need to pick pairs from the group such that pair sum can be divisible by 60

现在我们需要从组中选择对,以便对和可以被60整除

How can we pick?

我们如何挑选?

  1. Pick any from group[1] and group [59] //for first no remainder is 1, second remainder is 59 (1+59=60, divisible by 60)

    从组[1]和组[59]中选择任意一个// //首先没有余数是1,第二个余数是59(1 + 59 = 60,可被60整除)

  2. Pick any from group[2] and group [58] //for first no remainder is 2, second remainder is 58 (2+58=60, divisible by 60)

    从组[2]和组[58]中选择任何一个,//首先没有余数是2,第二个余数是58(2 + 58 = 60,可被60整除)

  3. Pick any from group[3] and group [57] //for first no remainder is 3, second remainder is 57(3+57=60, divisible by 60)

    从组[3]和组[57]中选择任意一个//首先,没有余数是3,第二个余数是57(3 + 57 = 60,可被60整除)

......................continue till group[29] and group[31]......................

......................继续到第[29] 组和第[31]组 。 .....

Now two groups are remaining
group[30] and group[60]
This two groups are independent group
We can pick any two elements from group[30]
Same for group[0]
We are done...

现在剩下两组
小组[30]和小组[60]
这两个小组是独立小组
我们可以从组[30]中选择任意两个元素
群组相同[0]
我们完了...

For group[30] and group[0]

对于组[30]和组[0]

Possible combinations are (n/2) where n be the respective values for group[30] and group[0]
And for 1-29 condition
Pick one from first group and one from second group
Which is n1*n2 //n1=first group item no, n2=second group item no

可能的组合是(n / 2) ,其中n是group [30]和group [0]的相应值
对于1-29条件
从第一组中选择一个,从第二组中选择一个
这是n1 * n2 // // n1 =第一组项目编号, n2 =第二组项目编号

For the above example

对于上面的例子

Only combination possible is from

只能组合来自

  1. group[10] and group[50] //1,1 elements respectively

    group [10]和group [50] // 1,1个元素

  2. group[0] //2 elements

    group [0] // 2个元素

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int numPairsDivisibleBy60(vector<int> time) {//group[60] renamed as a[60]
int count=0;
int a[60]={0};
for(int i=0;i<time.size();i++){a[time[i]%60]++;
}
int i=1,j=59;
while(i<j){ //for rules 1-29
count+=a[i]*a[j];
i++;
j--;
}
//for group[30] and group[0]
count+=(a[0]*(a[0]-1)/2)+(a[30]*(a[30]-1)/2);
return count;
}
int main(){int n,item;
cout<<"Number of times to be entered:\n";
cin>>n;
cout<<"Enter times...\n";
vector<int> time;
while(n--){cin>>item;
time.push_back(item);
}
cout<<"number of such pairs possible is: "
cout<<numPairsDivisibleBy60(time)<<endl;
return 0;
}

Output

输出量

Number of times to be entered:
7
Enter times...
10 20 30 60 80 110 120
number of such pairs possible is: 2

翻译自: https://www.includehelp.com/icp/pairs-of-songs-with-total-durations-divisible-by-60.aspx

成对的歌曲,其总持续时间可被60整除相关推荐

  1. LeetCode 1010. 总持续时间可被 60 整除的歌曲(哈希)

    1. 题目 在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒. 返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量.形式上,我们希望索引的数字 i < j 且有 (time ...

  2. 网上的音乐怎么下载成mp3格式歌曲?这3种一键下载的方法亲测好用!

    网易云音乐客户端是支持直接下载音乐的,但是有时候是加密格式,今天就教大家3个直接网页下载mp3格式音乐的方法,下载之后直接就能播放,亲测好用! 方法一:审查元素下载 一个简单有效的下载方法,不需要使用 ...

  3. LeetCode 700题 题解答案集合 Python

    2019.5.12更新至题号796,目前共有265题. 2019.5.26更新至题号72, 目前共有347题. 2019.6.16更新至题号70,目前共有382题. 2019.7.7更新至题号5120 ...

  4. 力扣(LeetCode)刷题,简单+中等题(第28期)

    目录 第1题:翻转单词顺序 第2题:顺时针打印矩阵 第3题:总持续时间可被 60 整除的歌曲 第4题:字符串的最大公因子 第5题:上升下降字符串 第6题:将数组分成和相等的三个部分 第7题:可被 5 ...

  5. LeetCode题解目录

    最新更新于2020.11.27 前往LeetCode主页. 前往GitHub源码.(服务器原因,暂停同步.) 前往码云主页. 已解决 456/1878 - 简单353 中等 90 困难 13 2020 ...

  6. 如何学好C语言--你的学渣朋友告诉你

    光说不练假把式,光练不说傻把式,又练又说真把式. 真实感受,不是我不想学,是我真的不知道我哪里不会啊和得怎么做啊?本文作者当初就是这么废掉的 推荐两个练习的网站 (一)C语言网 https://www ...

  7. Java算法:LeetCode算法Java版合集513-1110题

    513. 找树左下角的值 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入:2/ \1 3 输出: 1 示例 2: 输入:1/ \2 3/ / \4 5 6/7 输出: 7 ...

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

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

  9. LeetCode每日一题(持续更新中~~~)

    文章目录 2432. 处理用时最长的那个任务的员工5.5 1419. 数青蛙5.6 1010. 总持续时间可被 60 整除的歌曲5.7 2432. 处理用时最长的那个任务的员工5.5 共有 n 位员工 ...

最新文章

  1. Google AI的焦虑:拆分搜索和人工智能部门,Jeff Dean任AI业务负责人
  2. RookeyFrame2.0发布,UI重构
  3. 清华大学计算机学院赵成钢哪里人,清华大学16位学霸简历吓坏网友
  4. SAP Spartacus里的产品主数据显示的数据源
  5. P1232 [NOI2013] 树的计数
  6. SpringBoot整合Shiro权限框架
  7. Spring Security(四) —— 核心过滤器源码分析
  8. 统计各部门的薪水总和_近年来,统计学热过金融火过计算机,这是真的吗?
  9. building for iOS simulator, but linking in object file built for tvOS, for architecture x86_64
  10. 带你认识不一样的Stream,Java8就该这么玩!
  11. 拓端tecdat|R语言使用 LOWESS技术图分析逻辑回归中的函数形式
  12. ubuntu16.04下怎么安装flash player
  13. 白话空间统计之二十五:空间权重矩阵(三)解构空间权重矩阵
  14. 解决confuserEx混淆导致类名消失的问题
  15. Revit二次开发——族的基础
  16. 城市历年人均GDP API数据接口
  17. pygame画圆练习赤橙黄绿青蓝紫
  18. 怎样发表期刊才能快速通过
  19. 【参赛作品31】openGauss和PostgreSQL的源码目录结构对比
  20. API接口使用方法(封装好的电商平台)

热门文章

  1. canvas图表(4) - 散点图
  2. 如何用纯 CSS 创作一个冒着热气的咖啡杯
  3. 利用border制作三角形原理
  4. What?一个 Dubbo 服务启动要两个小时!
  5. 虚拟机Linux图形界面配置NAT-桥接
  6. 总线接口与计算机通信
  7. WebApp 里Meta标签大全
  8. linux 命令案例学习——文件搜索
  9. 2012服务器系统关闭网络共享,提供网络服务的前提,Windows Server 2012如何更改高级共享设置...
  10. vue.js根据数据循环生成表格_vue.js循环for(列表渲染)详解