成對的歌曲,其總持續時間可被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.pswp.cn/news/379878.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379878.shtml
英文地址,請注明出處:http://en.pswp.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;這里仍不能正確處…