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

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个元素

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379878.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt中QTableWidget用法总结

QTableWidget是QT程序中常用的显示数据表格的空间&#xff0c;很类似于VC、C#中的DataGrid。说到QTableWidget&#xff0c;就必须讲一下它跟QTabelView的区别了。QTableWidget是QTableView的子类&#xff0c;主要的区别是QTableView可以使用自定义的数据模型来显示内容(也就是先…

[转]软件架构师书单

"其实中国程序员&#xff0c;现在最需要的是一张安静的书桌。"&#xff0c;的确&#xff0c;中国架构师大多缺乏系统的基础知识&#xff0c;与其自欺欺人的宣扬"读书无用&#xff0c;重在实践变通&#xff0c;修身立命哲学书更重要"&#xff0c;把大好时间…

Java——List集合特有的功能

* List也是一个接口&#xff0c;这说明List不能new&#xff0c;其中它有一个子类ArrayList&#xff0c;所以&#xff0c;就可以父类引用指向子类对象调用* List里面特有的方法&#xff1a;* * void add(int index,E element)在列表的指定位置插入指定元素&#xff08;可选操作&…

python免杀技术---复现+改进----1

0x01 复现 复现文章&#xff1a;https://mp.weixin.qq.com/s?__bizMzI3MzUwMTQwNg&mid2247484733&idx2&sn5b8f439c2998ce089eb44541d2da7a15&chksmeb231%E2%80%A6 首先用cobaltstruke生成一个python的payload脚本 然后复制里面的payload进行Base64编码&…

python掷骰子_用于掷骰子的Python程序(2人骰子游戏)

python掷骰子Here, we will be going to design a very simple and easy game and implement it using abstract data class. The code consists of two different classes (The base of the whole program). The one will be the class for the player and others will be for…

ForeignKey和ManyToManyField的限制关系

authorsmodels.ManyToManyField(Author,limit_choice_to{name__endswith:Smith}这样可以更方便的查询。转载于:https://www.cnblogs.com/chenjianhong/archive/2012/03/22/4145158.html

linux 目录命令_Linux目录命令能力问题和解答

linux 目录命令This section contains Aptitude Questions and Answers on Linux Directory Commands. 本节包含有关Linux目录命令的 Aptitude问答。 1) There are the following statements that are given which of them are correct about Linux commands? In the Linux o…

终于在HP2133上成功安装xp

今天拿到一台HP2133迷你笔记本&#xff0c;原装vista home basic&#xff0c;由于本人是在不喜欢vista&#xff0c;于是决定将使用xp换之。 很久没有研究装系统了&#xff0c;HP2133没有光驱&#xff0c;以前也没啥这方面经验&#xff0c;搞这个玩意安装完软件折腾了大半天&…

Java——GUI(图形用户界面设计)

事件处理&#xff1a;事件&#xff1a;用户的一个操作(例如&#xff1a;点击一下鼠标&#xff0c;或者敲击一下键盘)事件源&#xff1a;被操作的组件(例如&#xff1a;在一个窗体中的一个按钮&#xff0c;那个按钮就属于被操作的组件&#xff0c;按钮就是事件源)监听器&#xf…

python安全攻防---信息收集---IP查询

IP查询是通过当前所获得的URL去查询对应IP地址的过程&#xff0c;可应用Socket库函数中的gethostbyname()获取域名所对用的IP值 程序如下&#xff1a; # -*- coding:utf-8 -*- IP查询import socket ip socket.gethostbyname(www.baidu.com) print(ip)运行结果&#xff1a; …

智能课程表Android版-学年学期星期的实现

上次我们实现了日期和时间的动态显示&#xff0c;这次我们来实现学年&#xff0c;学期&#xff0c;周次的显示&#xff0c;如图: 首先是学年学期的显示&#xff1a; Calendar cCalendar.getInstance(); int yearc.get(Calendar.YEAR); int monthc.get(Calendar.MONTH)1;//Calen…

感染linux脚本程序技术

前言 ---- 本文来源于29A病毒杂志,其上对linux shell病毒技术有了一个综合的阐述,我不想翻译它,我以它的那篇为模板 写了这篇中文的文章,里面的代码我都做了调试. 对于shell编程的程序员来说所谓的shell病毒技术其实根本就是小牛一毛,这点在大家看完本文后就会有所体会 但,简单…

Java——设计模式(简单工厂模式)

* A:简单工厂模式概述* 简单工厂模式又叫静态工厂方法模式&#xff0c;它定义了一个具体的工厂类负责创建一些类的实例* B&#xff1a;优点* 客户端不需要再负责对象的创建&#xff0c;从而明确了各个类的职责* 简单来说&#xff0c;客户端你只需要用就可以了&#xff0c;就…

Java ObjectOutputStream writeFloat()方法与示例

ObjectOutputStream类writeFloat()方法 (ObjectOutputStream Class writeFloat() method) writeFloat() method is available in java.io package. 在java.io包中提供了writeFloat()方法 。 writeFloat() method is used to write the given 4 bytes of a float value. writeFl…

python安全攻防---信息收集---whois查询

whois是用来查询域名的IP以及所有者信息的传输协议。简单地说&#xff0c;whois就是一个数据库&#xff0c;用来查询域名是否以及被注册&#xff0c;以及注册域名的详细信息&#xff08;如域名所有人、域名注册商等&#xff09;。 使用whois查询&#xff0c;首先通过pip安装py…

百度面试题:从输入url到显示网页,后台发生了什么?

参考http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ 作为一个软件开发者&#xff0c;你一定会…

VS2005无法启动修复办法

c:\Program Files\Microsoft Visual Studio 8\Common7\IDE>devenv /ResetSkipPkgs 转载于:https://www.cnblogs.com/doc/archive/2008/10/10/1307887.html

Java——设计模式(工厂方法模式)

* A:工厂方法模式概述* 工厂方法模式中抽象工厂类负责定义创建对象的接口&#xff0c;具体对象的创建工作由继承抽象工厂的具体类实现。* 简单来说&#xff1a;先定义一个工厂&#xff0c;工厂里面有些方法&#xff0c;这些方法就是用来创建动物的&#xff0c;然后有很多子工…

python安全攻防---爬虫基础---get和post提交数据

get提交数据1 get提交的数据就附在提交给服务器的url之后&#xff0c;以&#xff1f;开头参数之间以&隔开&#xff0c;例如/admin/user/123456.aspx?name123&id123 案例&#xff1a;写个脚本&#xff0c;在sogou自动搜索周杰伦&#xff0c;并将搜索页面的数据获取 程…

JavaMail中解决中文附件名乱码的问题

网上有很多类似的解决方案&#xff0c;很多是使用 if ((fileName ! null) && (fileName.toLowerCase().indexOf("gb2312") ! -1)){ fileName MimeUtility.decodeText(fileName); } 来解决&#xff0c;但对应gbk编码的附件名&#xff0c;这里仍不能正确处…