Skip to content

Commit 10ae25f

Browse files
committed
feat: add solutions to lc problem: No.1010
No.1010.Pairs of Songs With Total Durations Divisible by 60
1 parent a5659f9 commit 10ae25f

File tree

6 files changed

+198
-21
lines changed

6 files changed

+198
-21
lines changed

solution/1000-1099/1010.Pairs of Songs With Total Durations Divisible by 60/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,98 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:计数 + 枚举**
48+
49+
我们可以用一个哈希表或数组 `cnt` 记录当前已经遍历过的歌曲的持续时间 `time[i]` 的数量。
50+
51+
枚举当前遍历到的歌曲的持续时间 `time[i]`,假设其为 `t`,那么我们只需要枚举 `60` 的倍数 `s`,并统计 `cnt[s - t]` 即可。然后将 `cnt[t]` 的值加 `1`
52+
53+
时间复杂度 $O(n \times C)$,空间复杂度 $O(M)$。其中 $n$ 为数组 `time` 的长度,而 $C$ 和 $M$ 分别为数组 `time` 中的最大值以及 `60` 的倍数的个数。
54+
4755
<!-- tabs:start -->
4856

4957
### **Python3**
5058

5159
<!-- 这里可写当前语言的特殊实现逻辑 -->
5260

5361
```python
54-
62+
class Solution:
63+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
64+
cnt = defaultdict(int)
65+
ans = 0
66+
for t in time:
67+
s = 60
68+
for _ in range(17):
69+
ans += cnt[s - t]
70+
s += 60
71+
cnt[t] += 1
72+
return ans
5573
```
5674

5775
### **Java**
5876

5977
<!-- 这里可写当前语言的特殊实现逻辑 -->
6078

6179
```java
80+
class Solution {
81+
public int numPairsDivisibleBy60(int[] time) {
82+
int[] cnt = new int[501];
83+
int ans = 0;
84+
for (int t : time) {
85+
int s = 60;
86+
for (int i = 0; i < 17; ++i) {
87+
if (s - t >= 0 && s - t < cnt.length) {
88+
ans += cnt[s - t];
89+
}
90+
s += 60;
91+
}
92+
cnt[t]++;
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int numPairsDivisibleBy60(vector<int>& time) {
105+
int cnt[501]{};
106+
int ans = 0;
107+
for (int& t : time) {
108+
int s = 60;
109+
for (int i = 0; i < 17; ++i) {
110+
if (s - t >= 0 && s - t < 501) {
111+
ans += cnt[s - t];
112+
}
113+
s += 60;
114+
}
115+
cnt[t]++;
116+
}
117+
return ans;
118+
}
119+
};
120+
```
62121
122+
### **Go**
123+
124+
```go
125+
func numPairsDivisibleBy60(time []int) (ans int) {
126+
cnt := [501]int{}
127+
for _, t := range time {
128+
s := 60
129+
for i := 0; i < 17; i++ {
130+
if s-t >= 0 && s-t < 501 {
131+
ans += cnt[s-t]
132+
}
133+
s += 60
134+
}
135+
cnt[t]++
136+
}
137+
return
138+
}
63139
```
64140

65141
### **...**

solution/1000-1099/1010.Pairs of Songs With Total Durations Divisible by 60/README_EN.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,81 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
48+
cnt = defaultdict(int)
49+
ans = 0
50+
for t in time:
51+
s = 60
52+
for _ in range(17):
53+
ans += cnt[s - t]
54+
s += 60
55+
cnt[t] += 1
56+
return ans
4757
```
4858

4959
### **Java**
5060

5161
```java
62+
class Solution {
63+
public int numPairsDivisibleBy60(int[] time) {
64+
int[] cnt = new int[501];
65+
int ans = 0;
66+
for (int t : time) {
67+
int s = 60;
68+
for (int i = 0; i < 17; ++i) {
69+
if (s - t >= 0 && s - t < cnt.length) {
70+
ans += cnt[s - t];
71+
}
72+
s += 60;
73+
}
74+
cnt[t]++;
75+
}
76+
return ans;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int numPairsDivisibleBy60(vector<int>& time) {
87+
int cnt[501]{};
88+
int ans = 0;
89+
for (int& t : time) {
90+
int s = 60;
91+
for (int i = 0; i < 17; ++i) {
92+
if (s - t >= 0 && s - t < 501) {
93+
ans += cnt[s - t];
94+
}
95+
s += 60;
96+
}
97+
cnt[t]++;
98+
}
99+
return ans;
100+
}
101+
};
102+
```
52103
104+
### **Go**
105+
106+
```go
107+
func numPairsDivisibleBy60(time []int) (ans int) {
108+
cnt := [501]int{}
109+
for _, t := range time {
110+
s := 60
111+
for i := 0; i < 17; i++ {
112+
if s-t >= 0 && s-t < 501 {
113+
ans += cnt[s-t]
114+
}
115+
s += 60
116+
}
117+
cnt[t]++
118+
}
119+
return
120+
}
53121
```
54122

55123
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int numPairsDivisibleBy60(vector<int>& time) {
4+
int cnt[501]{};
5+
int ans = 0;
6+
for (int& t : time) {
7+
int s = 60;
8+
for (int i = 0; i < 17; ++i) {
9+
if (s - t >= 0 && s - t < 501) {
10+
ans += cnt[s - t];
11+
}
12+
s += 60;
13+
}
14+
cnt[t]++;
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func numPairsDivisibleBy60(time []int) (ans int) {
2+
cnt := [501]int{}
3+
for _, t := range time {
4+
s := 60
5+
for i := 0; i < 17; i++ {
6+
if s-t >= 0 && s-t < 501 {
7+
ans += cnt[s-t]
8+
}
9+
s += 60
10+
}
11+
cnt[t]++
12+
}
13+
return
14+
}
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
class Solution {
22
public int numPairsDivisibleBy60(int[] time) {
3-
Arrays.sort(time);
3+
int[] cnt = new int[501];
44
int ans = 0;
5-
for (int i = 0; i < time.length - 1; i++) {
6-
int num = (time[i] + time[time.length - 1]) / 60;
7-
8-
while (num > 0) {
9-
int key = num * 60;
10-
int index = Arrays.binarySearch(time, i + 1, time.length, key - time[i]);
11-
if (index >= 0) {
12-
int temp = index;
13-
ans++;
14-
while (++temp < time.length && time[temp] == time[index]) {
15-
ans++;
16-
}
17-
temp = index;
18-
while (--temp > i && time[temp] == time[index]) {
19-
ans++;
20-
}
5+
for (int t : time) {
6+
int s = 60;
7+
for (int i = 0; i < 17; ++i) {
8+
if (s - t >= 0 && s - t < cnt.length) {
9+
ans += cnt[s - t];
2110
}
22-
num--;
11+
s += 60;
2312
}
13+
cnt[t]++;
2414
}
2515
return ans;
2616
}
27-
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
3+
cnt = defaultdict(int)
4+
ans = 0
5+
for t in time:
6+
s = 60
7+
for _ in range(17):
8+
ans += cnt[s - t]
9+
s += 60
10+
cnt[t] += 1
11+
return ans

0 commit comments

Comments
 (0)