Skip to content

Commit 32a5c60

Browse files
committed
Add solution 1010
1 parent 2a94da0 commit 32a5c60

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func numPairsDivisibleBy60(time []int) int {
4+
counts := make([]int, 60)
5+
for _, v := range time {
6+
v %= 60
7+
counts[v]++
8+
}
9+
res := 0
10+
for i := 1; i < len(counts)/2; i++ {
11+
res += counts[i] * counts[60-i]
12+
}
13+
res += (counts[0] * (counts[0] - 1)) / 2
14+
res += (counts[30] * (counts[30] - 1)) / 2
15+
return res
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1010 struct {
9+
para1010
10+
ans1010
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1010 struct {
16+
time []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1010 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1010(t *testing.T) {
26+
27+
qs := []question1010{
28+
29+
{
30+
para1010{[]int{30, 20, 150, 100, 40}},
31+
ans1010{3},
32+
},
33+
34+
{
35+
para1010{[]int{60, 60, 60}},
36+
ans1010{3},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 1010------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans1010, q.para1010
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, numPairsDivisibleBy60(p.time))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# [1010. Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)
2+
3+
4+
## 题目
5+
6+
You are given a list of songs where the ith song has a duration of `time[i]` seconds.
7+
8+
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` such that `i < j` with `(time[i] + time[j]) % 60 == 0`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: time = [30,20,150,100,40]
14+
Output: 3
15+
Explanation: Three pairs have a total duration divisible by 60:
16+
(time[0] = 30, time[2] = 150): total duration 180
17+
(time[1] = 20, time[3] = 100): total duration 120
18+
(time[1] = 20, time[4] = 40): total duration 60
19+
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: time = [60,60,60]
26+
Output: 3
27+
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
28+
29+
```
30+
31+
**Constraints:**
32+
33+
- `1 <= time.length <= 6 * 104`
34+
- `1 <= time[i] <= 500`
35+
36+
## 题目大意
37+
38+
在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。
39+
40+
返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望下标数字 i 和 j 满足  i < j 且有 (time[i] + time[j]) % 60 == 0。
41+
42+
## 解题思路
43+
44+
- 简单题。先将数组每个元素对 60 取余,将它们都转换到 [0,59] 之间。然后在数组中找两两元素之和等于 60 的数对。可以在 0-30 之内对半查找符合条件的数对。对 0 和 30 单独计算。因为多个 0 相加,余数还为 0 。2 个 30 相加之和为 60。
45+
46+
## 代码
47+
48+
```go
49+
func numPairsDivisibleBy60(time []int) int {
50+
counts := make([]int, 60)
51+
for _, v := range time {
52+
v %= 60
53+
counts[v]++
54+
}
55+
res := 0
56+
for i := 1; i < len(counts)/2; i++ {
57+
res += counts[i] * counts[60-i]
58+
}
59+
res += (counts[0] * (counts[0] - 1)) / 2
60+
res += (counts[30] * (counts[30] - 1)) / 2
61+
return res
62+
}
63+
```

0 commit comments

Comments
 (0)