mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Add solution 1010
This commit is contained in:
@ -0,0 +1,16 @@
|
||||
package leetcode
|
||||
|
||||
func numPairsDivisibleBy60(time []int) int {
|
||||
counts := make([]int, 60)
|
||||
for _, v := range time {
|
||||
v %= 60
|
||||
counts[v]++
|
||||
}
|
||||
res := 0
|
||||
for i := 1; i < len(counts)/2; i++ {
|
||||
res += counts[i] * counts[60-i]
|
||||
}
|
||||
res += (counts[0] * (counts[0] - 1)) / 2
|
||||
res += (counts[30] * (counts[30] - 1)) / 2
|
||||
return res
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1010 struct {
|
||||
para1010
|
||||
ans1010
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1010 struct {
|
||||
time []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1010 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1010(t *testing.T) {
|
||||
|
||||
qs := []question1010{
|
||||
|
||||
{
|
||||
para1010{[]int{30, 20, 150, 100, 40}},
|
||||
ans1010{3},
|
||||
},
|
||||
|
||||
{
|
||||
para1010{[]int{60, 60, 60}},
|
||||
ans1010{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1010------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1010, q.para1010
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, numPairsDivisibleBy60(p.time))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
# [1010. Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a list of songs where the ith 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` such that `i < j` with `(time[i] + time[j]) % 60 == 0`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: time = [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: time = [60,60,60]
|
||||
Output: 3
|
||||
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= time.length <= 6 * 104`
|
||||
- `1 <= time[i] <= 500`
|
||||
|
||||
## 题目大意
|
||||
|
||||
在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。
|
||||
|
||||
返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望下标数字 i 和 j 满足 i < j 且有 (time[i] + time[j]) % 60 == 0。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。先将数组每个元素对 60 取余,将它们都转换到 [0,59] 之间。然后在数组中找两两元素之和等于 60 的数对。可以在 0-30 之内对半查找符合条件的数对。对 0 和 30 单独计算。因为多个 0 相加,余数还为 0 。2 个 30 相加之和为 60。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
func numPairsDivisibleBy60(time []int) int {
|
||||
counts := make([]int, 60)
|
||||
for _, v := range time {
|
||||
v %= 60
|
||||
counts[v]++
|
||||
}
|
||||
res := 0
|
||||
for i := 1; i < len(counts)/2; i++ {
|
||||
res += counts[i] * counts[60-i]
|
||||
}
|
||||
res += (counts[0] * (counts[0] - 1)) / 2
|
||||
res += (counts[30] * (counts[30] - 1)) / 2
|
||||
return res
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user