Skip to content

Commit 910f74e

Browse files
authored
feat: add solutions for lc No.1009,1010 (#5073)
1 parent 945c25c commit 910f74e

16 files changed

Lines changed: 134 additions & 287 deletions

File tree

solution/1000-1099/1009.Complement of Base 10 Integer/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ impl Solution {
179179
}
180180
```
181181

182+
#### C#
183+
184+
```cs
185+
public class Solution {
186+
public int BitwiseComplement(int n) {
187+
if (n == 0) {
188+
return 1;
189+
}
190+
int ans = 0, i = 0;
191+
while (n != 0) {
192+
ans |= ((n & 1) ^ 1) << (i++);
193+
n >>= 1;
194+
}
195+
return ans;
196+
}
197+
}
198+
```
199+
182200
<!-- tabs:end -->
183201

184202
<!-- solution:end -->

solution/1000-1099/1009.Complement of Base 10 Integer/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ impl Solution {
181181
}
182182
```
183183

184+
#### C#
185+
186+
```cs
187+
public class Solution {
188+
public int BitwiseComplement(int n) {
189+
if (n == 0) {
190+
return 1;
191+
}
192+
int ans = 0, i = 0;
193+
while (n != 0) {
194+
ans |= ((n & 1) ^ 1) << (i++);
195+
n >>= 1;
196+
}
197+
return ans;
198+
}
199+
}
200+
```
201+
184202
<!-- tabs:end -->
185203

186204
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int BitwiseComplement(int n) {
3+
if (n == 0) {
4+
return 1;
5+
}
6+
int ans = 0, i = 0;
7+
while (n != 0) {
8+
ans |= ((n & 1) ^ 1) << (i++);
9+
n >>= 1;
10+
}
11+
return ans;
12+
}
13+
}

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

Lines changed: 18 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -74,104 +74,6 @@ tags:
7474

7575
#### Python3
7676

77-
```python
78-
class Solution:
79-
def numPairsDivisibleBy60(self, time: List[int]) -> int:
80-
cnt = Counter(t % 60 for t in time)
81-
ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
82-
ans += cnt[0] * (cnt[0] - 1) // 2
83-
ans += cnt[30] * (cnt[30] - 1) // 2
84-
return ans
85-
```
86-
87-
#### Java
88-
89-
```java
90-
class Solution {
91-
public int numPairsDivisibleBy60(int[] time) {
92-
int[] cnt = new int[60];
93-
for (int t : time) {
94-
++cnt[t % 60];
95-
}
96-
int ans = 0;
97-
for (int x = 1; x < 30; ++x) {
98-
ans += cnt[x] * cnt[60 - x];
99-
}
100-
ans += (long) cnt[0] * (cnt[0] - 1) / 2;
101-
ans += (long) cnt[30] * (cnt[30] - 1) / 2;
102-
return ans;
103-
}
104-
}
105-
```
106-
107-
#### C++
108-
109-
```cpp
110-
class Solution {
111-
public:
112-
int numPairsDivisibleBy60(vector<int>& time) {
113-
int cnt[60]{};
114-
for (int& t : time) {
115-
++cnt[t % 60];
116-
}
117-
int ans = 0;
118-
for (int x = 1; x < 30; ++x) {
119-
ans += cnt[x] * cnt[60 - x];
120-
}
121-
ans += 1LL * cnt[0] * (cnt[0] - 1) / 2;
122-
ans += 1LL * cnt[30] * (cnt[30] - 1) / 2;
123-
return ans;
124-
}
125-
};
126-
```
127-
128-
#### Go
129-
130-
```go
131-
func numPairsDivisibleBy60(time []int) (ans int) {
132-
cnt := [60]int{}
133-
for _, t := range time {
134-
cnt[t%60]++
135-
}
136-
for x := 1; x < 30; x++ {
137-
ans += cnt[x] * cnt[60-x]
138-
}
139-
ans += cnt[0] * (cnt[0] - 1) / 2
140-
ans += cnt[30] * (cnt[30] - 1) / 2
141-
return
142-
}
143-
```
144-
145-
#### TypeScript
146-
147-
```ts
148-
function numPairsDivisibleBy60(time: number[]): number {
149-
const cnt: number[] = new Array(60).fill(0);
150-
for (const t of time) {
151-
++cnt[t % 60];
152-
}
153-
let ans = 0;
154-
for (let x = 1; x < 30; ++x) {
155-
ans += cnt[x] * cnt[60 - x];
156-
}
157-
ans += (cnt[0] * (cnt[0] - 1)) / 2;
158-
ans += (cnt[30] * (cnt[30] - 1)) / 2;
159-
return ans;
160-
}
161-
```
162-
163-
<!-- tabs:end -->
164-
165-
<!-- solution:end -->
166-
167-
<!-- solution:start -->
168-
169-
### 方法二
170-
171-
<!-- tabs:start -->
172-
173-
#### Python3
174-
17577
```python
17678
class Solution:
17779
def numPairsDivisibleBy60(self, time: List[int]) -> int:
@@ -253,6 +155,24 @@ function numPairsDivisibleBy60(time: number[]): number {
253155
}
254156
```
255157

158+
#### Rust
159+
160+
```rust
161+
impl Solution {
162+
pub fn num_pairs_divisible_by60(time: Vec<i32>) -> i32 {
163+
let mut cnt = [0i32; 60];
164+
let mut ans: i32 = 0;
165+
for mut x in time {
166+
x %= 60;
167+
let y = (60 - x) % 60;
168+
ans += cnt[y as usize];
169+
cnt[x as usize] += 1;
170+
}
171+
ans
172+
}
173+
}
174+
```
175+
256176
<!-- tabs:end -->
257177

258178
<!-- solution:end -->

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

Lines changed: 23 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -58,105 +58,15 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Math + Counting
6262

63-
<!-- tabs:start -->
64-
65-
#### Python3
66-
67-
```python
68-
class Solution:
69-
def numPairsDivisibleBy60(self, time: List[int]) -> int:
70-
cnt = Counter(t % 60 for t in time)
71-
ans = sum(cnt[x] * cnt[60 - x] for x in range(1, 30))
72-
ans += cnt[0] * (cnt[0] - 1) // 2
73-
ans += cnt[30] * (cnt[30] - 1) // 2
74-
return ans
75-
```
63+
If the sum of a pair $(a, b)$ is divisible by $60$, i.e., $(a + b) \bmod 60 = 0$, then $(a \bmod 60 + b \bmod 60) \bmod 60 = 0$. Let $x = a \bmod 60$ and $y = b \bmod 60$, then $(x + y) \bmod 60 = 0$, which means $y = (60 - x) \bmod 60$.
7664

77-
#### Java
78-
79-
```java
80-
class Solution {
81-
public int numPairsDivisibleBy60(int[] time) {
82-
int[] cnt = new int[60];
83-
for (int t : time) {
84-
++cnt[t % 60];
85-
}
86-
int ans = 0;
87-
for (int x = 1; x < 30; ++x) {
88-
ans += cnt[x] * cnt[60 - x];
89-
}
90-
ans += (long) cnt[0] * (cnt[0] - 1) / 2;
91-
ans += (long) cnt[30] * (cnt[30] - 1) / 2;
92-
return ans;
93-
}
94-
}
95-
```
65+
Therefore, we can iterate over the song list and use an array $cnt$ of length $60$ to record the number of occurrences of each remainder $x$. For the current $x$, if there exists a remainder $y = (60 - x) \bmod 60$ in array $cnt$, we add $cnt[y]$ to the answer. Then we increment the count of $x$ in array $cnt$ by $1$. We continue iterating until the entire song list has been traversed.
9666

97-
#### C++
67+
After the iteration, we get the number of song pairs that satisfy the condition.
9868

99-
```cpp
100-
class Solution {
101-
public:
102-
int numPairsDivisibleBy60(vector<int>& time) {
103-
int cnt[60]{};
104-
for (int& t : time) {
105-
++cnt[t % 60];
106-
}
107-
int ans = 0;
108-
for (int x = 1; x < 30; ++x) {
109-
ans += cnt[x] * cnt[60 - x];
110-
}
111-
ans += 1LL * cnt[0] * (cnt[0] - 1) / 2;
112-
ans += 1LL * cnt[30] * (cnt[30] - 1) / 2;
113-
return ans;
114-
}
115-
};
116-
```
117-
118-
#### Go
119-
120-
```go
121-
func numPairsDivisibleBy60(time []int) (ans int) {
122-
cnt := [60]int{}
123-
for _, t := range time {
124-
cnt[t%60]++
125-
}
126-
for x := 1; x < 30; x++ {
127-
ans += cnt[x] * cnt[60-x]
128-
}
129-
ans += cnt[0] * (cnt[0] - 1) / 2
130-
ans += cnt[30] * (cnt[30] - 1) / 2
131-
return
132-
}
133-
```
134-
135-
#### TypeScript
136-
137-
```ts
138-
function numPairsDivisibleBy60(time: number[]): number {
139-
const cnt: number[] = new Array(60).fill(0);
140-
for (const t of time) {
141-
++cnt[t % 60];
142-
}
143-
let ans = 0;
144-
for (let x = 1; x < 30; ++x) {
145-
ans += cnt[x] * cnt[60 - x];
146-
}
147-
ans += (cnt[0] * (cnt[0] - 1)) / 2;
148-
ans += (cnt[30] * (cnt[30] - 1)) / 2;
149-
return ans;
150-
}
151-
```
152-
153-
<!-- tabs:end -->
154-
155-
<!-- solution:end -->
156-
157-
<!-- solution:start -->
158-
159-
### Solution 2
69+
The time complexity is $O(n)$ and the space complexity is $O(C)$, where $n$ is the length of the song list and $C$ is the number of possible remainders, here $C = 60$.
16070

16171
<!-- tabs:start -->
16272

@@ -243,6 +153,24 @@ function numPairsDivisibleBy60(time: number[]): number {
243153
}
244154
```
245155

156+
#### Rust
157+
158+
```rust
159+
impl Solution {
160+
pub fn num_pairs_divisible_by60(time: Vec<i32>) -> i32 {
161+
let mut cnt = [0i32; 60];
162+
let mut ans: i32 = 0;
163+
for mut x in time {
164+
x %= 60;
165+
let y = (60 - x) % 60;
166+
ans += cnt[y as usize];
167+
cnt[x as usize] += 1;
168+
}
169+
ans
170+
}
171+
}
172+
```
173+
246174
<!-- tabs:end -->
247175

248176
<!-- solution:end -->

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ class Solution {
22
public:
33
int numPairsDivisibleBy60(vector<int>& time) {
44
int cnt[60]{};
5-
for (int& t : time) {
6-
++cnt[t % 60];
7-
}
85
int ans = 0;
9-
for (int x = 1; x < 30; ++x) {
10-
ans += cnt[x] * cnt[60 - x];
6+
for (int x : time) {
7+
x %= 60;
8+
int y = (60 - x) % 60;
9+
ans += cnt[y];
10+
++cnt[x];
1111
}
12-
ans += 1LL * cnt[0] * (cnt[0] - 1) / 2;
13-
ans += 1LL * cnt[30] * (cnt[30] - 1) / 2;
1412
return ans;
1513
}
16-
};
14+
};
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
func numPairsDivisibleBy60(time []int) (ans int) {
22
cnt := [60]int{}
3-
for _, t := range time {
4-
cnt[t%60]++
3+
for _, x := range time {
4+
x %= 60
5+
y := (60 - x) % 60
6+
ans += cnt[y]
7+
cnt[x]++
58
}
6-
for x := 1; x < 30; x++ {
7-
ans += cnt[x] * cnt[60-x]
8-
}
9-
ans += cnt[0] * (cnt[0] - 1) / 2
10-
ans += cnt[30] * (cnt[30] - 1) / 2
119
return
12-
}
10+
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public int numPairsDivisibleBy60(int[] time) {
33
int[] cnt = new int[60];
4-
for (int t : time) {
5-
++cnt[t % 60];
6-
}
74
int ans = 0;
8-
for (int x = 1; x < 30; ++x) {
9-
ans += cnt[x] * cnt[60 - x];
5+
for (int x : time) {
6+
x %= 60;
7+
int y = (60 - x) % 60;
8+
ans += cnt[y];
9+
++cnt[x];
1010
}
11-
ans += (long) cnt[0] * (cnt[0] - 1) / 2;
12-
ans += (long) cnt[30] * (cnt[30] - 1) / 2;
1311
return ans;
1412
}
15-
}
13+
}

0 commit comments

Comments
 (0)