Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61857c7

Browse files
committed
feat: add solutions to lc problem: No.1010
No.1010.Pairs of Songs With Total Durations Divisible by 60
1 parent 7129eb8 commit 61857c7

File tree

7 files changed

+280
-113
lines changed

7 files changed

+280
-113
lines changed

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

Lines changed: 123 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444

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

47-
**方法一:计数 + 枚举**
47+
**方法一:数学 + 计数**
4848

49-
我们可以用一个哈希表或数组 `cnt` 记录当前已经遍历过的歌曲的持续时间 `time[i]` 的数量
49+
如果一个数对 $(a, b)$ 之和能被 60ドル$ 整除,即 $(a + b) \bmod 60 = 0,ドル那么 $(a \bmod 60 + b \bmod 60) \bmod 60 = 0,ドル不妨记 $x=a \bmod 60,ドル $y = b \bmod 60,ドル那么有 $(x + y) \bmod 60 = 0,ドル即 $y=(60 - x) \bmod 60$
5050

51-
枚举当前遍历到的歌曲的持续时间 `time[i]`,假设其为 `t`,那么我们只需要枚举 `60` 的倍数 `s`,并统计 `cnt[s - t]` 即可。然后将 `cnt[t]` 的值加 `1`
51+
因此,我们可以遍历歌曲列表,用一个长度为 60ドル$ 的数组 $cnt$ 记录每个余数 $x$ 出现的次数。对于当前的 $x,ドル如果数组 $cnt$ 中存在余数 $y = (60 - x) \bmod 60,ドル那么将 $cnt[y]$ 累加进答案中。然后,将 $x$ 在数组 $cnt$ 中的出现次数加 1ドル$。继续遍历,直到遍历完整个歌曲列表
5252

53-
时间复杂度 $O(n \times C),ドル空间复杂度 $O(M)$。其中 $n$ 为数组 `time` 的长度,而 $C$ 和 $M$ 分别为数组 `time` 中的最大值以及 `60` 的倍数的个数。
53+
遍历结束后,即可得到满足条件的歌曲对数目。
54+
55+
时间复杂度 $O(n),ドル空间复杂度 $O(C)$。其中 $n$ 是歌曲列表的长度;而 $C$ 是余数的可能取值,这里 $C=60$。
5456

5557
<!-- tabs:start -->
5658

@@ -61,14 +63,23 @@
6163
```python
6264
class Solution:
6365
def numPairsDivisibleBy60(self, time: List[int]) -> int:
64-
cnt = defaultdict(int)
66+
cnt = Counter(t % 60 for t in time)
67+
ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
68+
ans += cnt[0] * (cnt[0] - 1) // 2
69+
ans += cnt[30] * (cnt[30] - 1) // 2
70+
return ans
71+
```
72+
73+
```python
74+
class Solution:
75+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
76+
cnt = Counter()
6577
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
78+
for x in time:
79+
x %= 60
80+
y = (60 - x) % 60
81+
ans += cnt[y]
82+
cnt[x] += 1
7283
return ans
7384
```
7485

@@ -79,17 +90,31 @@ class Solution:
7990
```java
8091
class Solution {
8192
public int numPairsDivisibleBy60(int[] time) {
82-
int[] cnt = new int[501];
83-
int ans = 0;
93+
int[] cnt = new int[60];
8494
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]++;
95+
++cnt[t % 60];
96+
}
97+
int ans = 0;
98+
for (int x = 1; x < 30; ++x) {
99+
ans += cnt[x] * cnt[60 - x];
100+
}
101+
ans += (long) cnt[0] * (cnt[0] - 1) / 2;
102+
ans += (long) cnt[30] * (cnt[30] - 1) / 2;
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
```java
109+
class Solution {
110+
public int numPairsDivisibleBy60(int[] time) {
111+
int[] cnt = new int[60];
112+
int ans = 0;
113+
for (int x : time) {
114+
x %= 60;
115+
int y = (60 - x) % 60;
116+
ans += cnt[y];
117+
++cnt[x];
93118
}
94119
return ans;
95120
}
@@ -102,17 +127,32 @@ class Solution {
102127
class Solution {
103128
public:
104129
int numPairsDivisibleBy60(vector<int>& time) {
105-
int cnt[501]{};
106-
int ans = 0;
130+
int cnt[60]{};
107131
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]++;
132+
++cnt[t % 60];
133+
}
134+
int ans = 0;
135+
for (int x = 1; x < 30; ++x) {
136+
ans += cnt[x] * cnt[60 - x];
137+
}
138+
ans += 1LL * cnt[0] * (cnt[0] - 1) / 2;
139+
ans += 1LL * cnt[30] * (cnt[30] - 1) / 2;
140+
return ans;
141+
}
142+
};
143+
```
144+
145+
```cpp
146+
class Solution {
147+
public:
148+
int numPairsDivisibleBy60(vector<int>& time) {
149+
int cnt[60]{};
150+
int ans = 0;
151+
for (int x : time) {
152+
x %= 60;
153+
int y = (60 - x) % 60;
154+
ans += cnt[y];
155+
++cnt[x];
116156
}
117157
return ans;
118158
}
@@ -123,21 +163,64 @@ public:
123163

124164
```go
125165
func numPairsDivisibleBy60(time []int) (ans int) {
126-
cnt := [501]int{}
166+
cnt := [60]int{}
127167
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]++
168+
cnt[t%60]++
169+
}
170+
for x := 1; x < 30; x++ {
171+
ans += cnt[x] * cnt[60-x]
172+
}
173+
ans += cnt[0] * (cnt[0] - 1) / 2
174+
ans += cnt[30] * (cnt[30] - 1) / 2
175+
return
176+
}
177+
```
178+
179+
```go
180+
func numPairsDivisibleBy60(time []int) (ans int) {
181+
cnt := [60]int{}
182+
for _, x := range time {
183+
x %= 60
184+
y := (60 - x) % 60
185+
ans += cnt[y]
186+
cnt[x]++
136187
}
137188
return
138189
}
139190
```
140191

192+
### **TypeScript**
193+
194+
```ts
195+
function numPairsDivisibleBy60(time: number[]): number {
196+
const cnt: number[] = new Array(60).fill(0);
197+
for (const t of time) {
198+
++cnt[t % 60];
199+
}
200+
let ans = 0;
201+
for (let x = 1; x < 30; ++x) {
202+
ans += cnt[x] * cnt[60 - x];
203+
}
204+
ans += (cnt[0] * (cnt[0] - 1)) / 2;
205+
ans += (cnt[30] * (cnt[30] - 1)) / 2;
206+
return ans;
207+
}
208+
```
209+
210+
```ts
211+
function numPairsDivisibleBy60(time: number[]): number {
212+
const cnt: number[] = new Array(60).fill(0);
213+
let ans: number = 0;
214+
for (let x of time) {
215+
x %= 60;
216+
const y = (60 - x) % 60;
217+
ans += cnt[y];
218+
++cnt[x];
219+
}
220+
return ans;
221+
}
222+
```
223+
141224
### **...**
142225

143226
```

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

Lines changed: 117 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,23 @@
4545
```python
4646
class Solution:
4747
def numPairsDivisibleBy60(self, time: List[int]) -> int:
48-
cnt = defaultdict(int)
48+
cnt = Counter(t % 60 for t in time)
49+
ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
50+
ans += cnt[0] * (cnt[0] - 1) // 2
51+
ans += cnt[30] * (cnt[30] - 1) // 2
52+
return ans
53+
```
54+
55+
```python
56+
class Solution:
57+
def numPairsDivisibleBy60(self, time: List[int]) -> int:
58+
cnt = Counter()
4959
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
60+
for x in time:
61+
x %= 60
62+
y = (60 - x) % 60
63+
ans += cnt[y]
64+
cnt[x] += 1
5665
return ans
5766
```
5867

@@ -61,17 +70,31 @@ class Solution:
6170
```java
6271
class Solution {
6372
public int numPairsDivisibleBy60(int[] time) {
64-
int[] cnt = new int[501];
65-
int ans = 0;
73+
int[] cnt = new int[60];
6674
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+
++cnt[t % 60];
76+
}
77+
int ans = 0;
78+
for (int x = 1; x < 30; ++x) {
79+
ans += cnt[x] * cnt[60 - x];
80+
}
81+
ans += (long) cnt[0] * (cnt[0] - 1) / 2;
82+
ans += (long) cnt[30] * (cnt[30] - 1) / 2;
83+
return ans;
84+
}
85+
}
86+
```
87+
88+
```java
89+
class Solution {
90+
public int numPairsDivisibleBy60(int[] time) {
91+
int[] cnt = new int[60];
92+
int ans = 0;
93+
for (int x : time) {
94+
x %= 60;
95+
int y = (60 - x) % 60;
96+
ans += cnt[y];
97+
++cnt[x];
7598
}
7699
return ans;
77100
}
@@ -84,17 +107,32 @@ class Solution {
84107
class Solution {
85108
public:
86109
int numPairsDivisibleBy60(vector<int>& time) {
87-
int cnt[501]{};
88-
int ans = 0;
110+
int cnt[60]{};
89111
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]++;
112+
++cnt[t % 60];
113+
}
114+
int ans = 0;
115+
for (int x = 1; x < 30; ++x) {
116+
ans += cnt[x] * cnt[60 - x];
117+
}
118+
ans += 1LL * cnt[0] * (cnt[0] - 1) / 2;
119+
ans += 1LL * cnt[30] * (cnt[30] - 1) / 2;
120+
return ans;
121+
}
122+
};
123+
```
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int numPairsDivisibleBy60(vector<int>& time) {
129+
int cnt[60]{};
130+
int ans = 0;
131+
for (int x : time) {
132+
x %= 60;
133+
int y = (60 - x) % 60;
134+
ans += cnt[y];
135+
++cnt[x];
98136
}
99137
return ans;
100138
}
@@ -105,21 +143,64 @@ public:
105143

106144
```go
107145
func numPairsDivisibleBy60(time []int) (ans int) {
108-
cnt := [501]int{}
146+
cnt := [60]int{}
109147
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]++
148+
cnt[t%60]++
118149
}
150+
for x := 1; x < 30; x++ {
151+
ans += cnt[x] * cnt[60-x]
152+
}
153+
ans += cnt[0] * (cnt[0] - 1) / 2
154+
ans += cnt[30] * (cnt[30] - 1) / 2
119155
return
120156
}
121157
```
122158

159+
```go
160+
func numPairsDivisibleBy60(time []int) (ans int) {
161+
cnt := [60]int{}
162+
for _, x := range time {
163+
x %= 60
164+
y := (60 - x) % 60
165+
ans += cnt[y]
166+
cnt[x]++
167+
}
168+
return
169+
}
170+
```
171+
172+
### **TypeScript**
173+
174+
```ts
175+
function numPairsDivisibleBy60(time: number[]): number {
176+
const cnt: number[] = new Array(60).fill(0);
177+
for (const t of time) {
178+
++cnt[t % 60];
179+
}
180+
let ans = 0;
181+
for (let x = 1; x < 30; ++x) {
182+
ans += cnt[x] * cnt[60 - x];
183+
}
184+
ans += (cnt[0] * (cnt[0] - 1)) / 2;
185+
ans += (cnt[30] * (cnt[30] - 1)) / 2;
186+
return ans;
187+
}
188+
```
189+
190+
```ts
191+
function numPairsDivisibleBy60(time: number[]): number {
192+
const cnt: number[] = new Array(60).fill(0);
193+
let ans: number = 0;
194+
for (let x of time) {
195+
x %= 60;
196+
const y = (60 - x) % 60;
197+
ans += cnt[y];
198+
++cnt[x];
199+
}
200+
return ans;
201+
}
202+
```
203+
123204
### **...**
124205

125206
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /