Skip to content

Commit a6cd01a

Browse files
authored
feat: update lc problems (#4134)
1 parent c99e4d6 commit a6cd01a

File tree

15 files changed

+160
-222
lines changed

15 files changed

+160
-222
lines changed

solution/2500-2599/2505.Bitwise OR of All Subsequence Sums/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ And we have 0 OR 1 OR 2 OR 3 OR 4 OR 5 OR 6 = 7, so we return 7.
5555

5656
<!-- solution:start -->
5757

58-
### Solution 1
58+
### Solution 1: Bit Manipulation
59+
60+
We first use an array $cnt$ to count the number of 1s in each bit position. Then, from the lowest bit to the highest bit, if the number of 1s in that bit position is greater than 0, we add the value represented by that bit to the answer. Then, we check if there can be a carry-over, and if so, we add it to the next bit.
61+
62+
The time complexity is $O(n \times \log M)$, where $n$ is the length of the array and $M$ is the maximum value in the array.
5963

6064
<!-- tabs:start -->
6165

solution/2500-2599/2507.Smallest Value After Replacing With Sum of Prime Factors/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Brute Force Simulation
69+
70+
According to the problem statement, we can perform a process of prime factorization, i.e., continuously decompose a number into its prime factors until it can no longer be decomposed. During the process, add the prime factors each time they are decomposed, and perform this recursively or iteratively.
71+
72+
The time complexity is $O(\sqrt{n})$.
6973

7074
<!-- tabs:start -->
7175

solution/2500-2599/2508.Add Edges to Make Degrees of All Nodes Even/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,19 @@ Every node in the resulting graph is connected to an even number of edges.
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Case Analysis
73+
74+
We first build the graph $g$ using `edges`, and then find all nodes with odd degrees, denoted as $vs$.
75+
76+
If the length of $vs$ is $0$, it means all nodes in the graph $g$ have even degrees, so we return `true`.
77+
78+
If the length of $vs$ is $2$, it means there are two nodes with odd degrees in the graph $g$. If we can directly connect these two nodes with an edge, making all nodes in the graph $g$ have even degrees, we return `true`. Otherwise, if we can find a third node $c$ such that we can connect $a$ and $c$, and $b$ and $c$, making all nodes in the graph $g$ have even degrees, we return `true`. Otherwise, we return `false`.
79+
80+
If the length of $vs$ is $4$, we enumerate all possible pairs and check if any combination meets the conditions. If so, we return `true`; otherwise, we return `false`.
81+
82+
In other cases, we return `false`.
83+
84+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the number of nodes and edges, respectively.
7385

7486
<!-- tabs:start -->
7587

solution/2500-2599/2509.Cycle Length Queries in a Tree/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ tags:
8484

8585
<!-- solution:start -->
8686

87-
### Solution 1
87+
### Solution 1: Finding the Lowest Common Ancestor
88+
89+
For each query, we find the lowest common ancestor of the two nodes $a$ and $b$, and record the number of steps taken upwards. The answer to the query is the number of steps plus one.
90+
91+
To find the lowest common ancestor, if $a > b$, we move $a$ to its parent node; if $a < b$, we move $b$ to its parent node. We accumulate the number of steps until $a = b$.
92+
93+
The time complexity is $O(n \times m)$, where $m$ is the length of the `queries` array.
8894

8995
<!-- tabs:start -->
9096

solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README.md

-37
Original file line numberDiff line numberDiff line change
@@ -221,41 +221,4 @@ impl Solution {
221221

222222
<!-- solution:end -->
223223

224-
<!-- solution:start -->
225-
226-
### 方法二
227-
228-
<!-- tabs:start -->
229-
230-
#### Rust
231-
232-
```rust
233-
impl Solution {
234-
pub fn capture_forts(forts: Vec<i32>) -> i32 {
235-
let mut ans = 0;
236-
let mut i = 0;
237-
238-
while let Some((idx, &value)) = forts.iter().enumerate().skip(i).find(|&(_, &x)| x != 0) {
239-
if let Some((jdx, _)) = forts
240-
.iter()
241-
.enumerate()
242-
.skip(idx + 1)
243-
.find(|&(_, &x)| x != 0)
244-
{
245-
if value + forts[jdx] == 0 {
246-
ans = ans.max(jdx - idx - 1);
247-
}
248-
}
249-
i = idx + 1;
250-
}
251-
252-
ans as i32
253-
}
254-
}
255-
```
256-
257-
<!-- tabs:end -->
258-
259-
<!-- solution:end -->
260-
261224
<!-- problem:end -->

solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README_EN.md

+5-38
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ Since 4 is the maximum number of enemy forts that can be captured, we return 4.
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Two Pointers
76+
77+
We use a pointer $i$ to traverse the array $forts$, and a pointer $j$ to start traversing from the next position of $i$ until it encounters the first non-zero position, i.e., $forts[j] \neq 0$. If $forts[i] + forts[j] = 0$, then we can move the army between $i$ and $j$, destroying $j - i - 1$ enemy forts. We use the variable $ans$ to record the maximum number of enemy forts that can be destroyed.
78+
79+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array `forts`.
7680

7781
<!-- tabs:start -->
7882

@@ -217,41 +221,4 @@ impl Solution {
217221

218222
<!-- solution:end -->
219223

220-
<!-- solution:start -->
221-
222-
### Solution 2
223-
224-
<!-- tabs:start -->
225-
226-
#### Rust
227-
228-
```rust
229-
impl Solution {
230-
pub fn capture_forts(forts: Vec<i32>) -> i32 {
231-
let mut ans = 0;
232-
let mut i = 0;
233-
234-
while let Some((idx, &value)) = forts.iter().enumerate().skip(i).find(|&(_, &x)| x != 0) {
235-
if let Some((jdx, _)) = forts
236-
.iter()
237-
.enumerate()
238-
.skip(idx + 1)
239-
.find(|&(_, &x)| x != 0)
240-
{
241-
if value + forts[jdx] == 0 {
242-
ans = ans.max(jdx - idx - 1);
243-
}
244-
}
245-
i = idx + 1;
246-
}
247-
248-
ans as i32
249-
}
250-
}
251-
```
252-
253-
<!-- tabs:end -->
254-
255-
<!-- solution:end -->
256-
257224
<!-- problem:end -->

solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/Solution2.rs

-22
This file was deleted.

solution/2500-2599/2514.Count Anagrams/README.md

+8-40
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,17 @@ tags:
7070
#### Python3
7171

7272
```python
73-
mod = 10**9 + 7
74-
f = [1]
75-
for i in range(1, 10**5 + 1):
76-
f.append(f[-1] * i % mod)
77-
78-
7973
class Solution:
8074
def countAnagrams(self, s: str) -> int:
81-
ans = 1
75+
mod = 10**9 + 7
76+
ans = mul = 1
8277
for w in s.split():
83-
cnt = Counter(w)
84-
ans *= f[len(w)]
85-
ans %= mod
86-
for v in cnt.values():
87-
ans *= pow(f[v], -1, mod)
88-
ans %= mod
89-
return ans
78+
cnt = Counter()
79+
for i, c in enumerate(w, 1):
80+
cnt[c] += 1
81+
mul = mul * cnt[c] % mod
82+
ans = ans * i % mod
83+
return ans * pow(mul, -1, mod) % mod
9084
```
9185

9286
#### Java
@@ -190,30 +184,4 @@ func pow(x, n int) int {
190184

191185
<!-- solution:end -->
192186

193-
<!-- solution:start -->
194-
195-
### 方法二
196-
197-
<!-- tabs:start -->
198-
199-
#### Python3
200-
201-
```python
202-
class Solution:
203-
def countAnagrams(self, s: str) -> int:
204-
mod = 10**9 + 7
205-
ans = mul = 1
206-
for w in s.split():
207-
cnt = Counter()
208-
for i, c in enumerate(w, 1):
209-
cnt[c] += 1
210-
mul = mul * cnt[c] % mod
211-
ans = ans * i % mod
212-
return ans * pow(mul, -1, mod) % mod
213-
```
214-
215-
<!-- tabs:end -->
216-
217-
<!-- solution:end -->
218-
219187
<!-- problem:end -->

solution/2500-2599/2514.Count Anagrams/README_EN.md

+8-40
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,17 @@ tags:
7070
#### Python3
7171

7272
```python
73-
mod = 10**9 + 7
74-
f = [1]
75-
for i in range(1, 10**5 + 1):
76-
f.append(f[-1] * i % mod)
77-
78-
7973
class Solution:
8074
def countAnagrams(self, s: str) -> int:
81-
ans = 1
75+
mod = 10**9 + 7
76+
ans = mul = 1
8277
for w in s.split():
83-
cnt = Counter(w)
84-
ans *= f[len(w)]
85-
ans %= mod
86-
for v in cnt.values():
87-
ans *= pow(f[v], -1, mod)
88-
ans %= mod
89-
return ans
78+
cnt = Counter()
79+
for i, c in enumerate(w, 1):
80+
cnt[c] += 1
81+
mul = mul * cnt[c] % mod
82+
ans = ans * i % mod
83+
return ans * pow(mul, -1, mod) % mod
9084
```
9185

9286
#### Java
@@ -190,30 +184,4 @@ func pow(x, n int) int {
190184

191185
<!-- solution:end -->
192186

193-
<!-- solution:start -->
194-
195-
### Solution 2
196-
197-
<!-- tabs:start -->
198-
199-
#### Python3
200-
201-
```python
202-
class Solution:
203-
def countAnagrams(self, s: str) -> int:
204-
mod = 10**9 + 7
205-
ans = mul = 1
206-
for w in s.split():
207-
cnt = Counter()
208-
for i, c in enumerate(w, 1):
209-
cnt[c] += 1
210-
mul = mul * cnt[c] % mod
211-
ans = ans * i % mod
212-
return ans * pow(mul, -1, mod) % mod
213-
```
214-
215-
<!-- tabs:end -->
216-
217-
<!-- solution:end -->
218-
219187
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
mod = 10**9 + 7
2-
f = [1]
3-
for i in range(1, 10**5 + 1):
4-
f.append(f[-1] * i % mod)
5-
6-
71
class Solution:
82
def countAnagrams(self, s: str) -> int:
9-
ans = 1
3+
mod = 10**9 + 7
4+
ans = mul = 1
105
for w in s.split():
11-
cnt = Counter(w)
12-
ans *= f[len(w)]
13-
ans %= mod
14-
for v in cnt.values():
15-
ans *= pow(f[v], -1, mod)
16-
ans %= mod
17-
return ans
6+
cnt = Counter()
7+
for i, c in enumerate(w, 1):
8+
cnt[c] += 1
9+
mul = mul * cnt[c] % mod
10+
ans = ans * i % mod
11+
return ans * pow(mul, -1, mod) % mod

solution/2500-2599/2514.Count Anagrams/Solution2.py

-11
This file was deleted.

0 commit comments

Comments
 (0)