Skip to content

feat: update lc problems #4134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- solution:start -->

### Solution 1
### Solution 1: Bit Manipulation

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.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Brute Force Simulation

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.

The time complexity is $O(\sqrt{n})$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ Every node in the resulting graph is connected to an even number of edges.

<!-- solution:start -->

### Solution 1
### Solution 1: Case Analysis

We first build the graph $g$ using `edges`, and then find all nodes with odd degrees, denoted as $vs$.

If the length of $vs$ is $0$, it means all nodes in the graph $g$ have even degrees, so we return `true`.

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`.

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`.

In other cases, we return `false`.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Finding the Lowest Common Ancestor

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.

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$.

The time complexity is $O(n \times m)$, where $m$ is the length of the `queries` array.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,41 +221,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn capture_forts(forts: Vec<i32>) -> i32 {
let mut ans = 0;
let mut i = 0;

while let Some((idx, &value)) = forts.iter().enumerate().skip(i).find(|&(_, &x)| x != 0) {
if let Some((jdx, _)) = forts
.iter()
.enumerate()
.skip(idx + 1)
.find(|&(_, &x)| x != 0)
{
if value + forts[jdx] == 0 {
ans = ans.max(jdx - idx - 1);
}
}
i = idx + 1;
}

ans as i32
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ Since 4 is the maximum number of enemy forts that can be captured, we return 4.

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

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.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array `forts`.

<!-- tabs:start -->

Expand Down Expand Up @@ -217,41 +221,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn capture_forts(forts: Vec<i32>) -> i32 {
let mut ans = 0;
let mut i = 0;

while let Some((idx, &value)) = forts.iter().enumerate().skip(i).find(|&(_, &x)| x != 0) {
if let Some((jdx, _)) = forts
.iter()
.enumerate()
.skip(idx + 1)
.find(|&(_, &x)| x != 0)
{
if value + forts[jdx] == 0 {
ans = ans.max(jdx - idx - 1);
}
}
i = idx + 1;
}

ans as i32
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->

This file was deleted.

48 changes: 8 additions & 40 deletions solution/2500-2599/2514.Count Anagrams/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,17 @@ tags:
#### Python3

```python
mod = 10**9 + 7
f = [1]
for i in range(1, 10**5 + 1):
f.append(f[-1] * i % mod)


class Solution:
def countAnagrams(self, s: str) -> int:
ans = 1
mod = 10**9 + 7
ans = mul = 1
for w in s.split():
cnt = Counter(w)
ans *= f[len(w)]
ans %= mod
for v in cnt.values():
ans *= pow(f[v], -1, mod)
ans %= mod
return ans
cnt = Counter()
for i, c in enumerate(w, 1):
cnt[c] += 1
mul = mul * cnt[c] % mod
ans = ans * i % mod
return ans * pow(mul, -1, mod) % mod
```

#### Java
Expand Down Expand Up @@ -190,30 +184,4 @@ func pow(x, n int) int {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def countAnagrams(self, s: str) -> int:
mod = 10**9 + 7
ans = mul = 1
for w in s.split():
cnt = Counter()
for i, c in enumerate(w, 1):
cnt[c] += 1
mul = mul * cnt[c] % mod
ans = ans * i % mod
return ans * pow(mul, -1, mod) % mod
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
48 changes: 8 additions & 40 deletions solution/2500-2599/2514.Count Anagrams/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,17 @@ tags:
#### Python3

```python
mod = 10**9 + 7
f = [1]
for i in range(1, 10**5 + 1):
f.append(f[-1] * i % mod)


class Solution:
def countAnagrams(self, s: str) -> int:
ans = 1
mod = 10**9 + 7
ans = mul = 1
for w in s.split():
cnt = Counter(w)
ans *= f[len(w)]
ans %= mod
for v in cnt.values():
ans *= pow(f[v], -1, mod)
ans %= mod
return ans
cnt = Counter()
for i, c in enumerate(w, 1):
cnt[c] += 1
mul = mul * cnt[c] % mod
ans = ans * i % mod
return ans * pow(mul, -1, mod) % mod
```

#### Java
Expand Down Expand Up @@ -190,30 +184,4 @@ func pow(x, n int) int {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def countAnagrams(self, s: str) -> int:
mod = 10**9 + 7
ans = mul = 1
for w in s.split():
cnt = Counter()
for i, c in enumerate(w, 1):
cnt[c] += 1
mul = mul * cnt[c] % mod
ans = ans * i % mod
return ans * pow(mul, -1, mod) % mod
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
22 changes: 8 additions & 14 deletions solution/2500-2599/2514.Count Anagrams/Solution.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
mod = 10**9 + 7
f = [1]
for i in range(1, 10**5 + 1):
f.append(f[-1] * i % mod)


class Solution:
def countAnagrams(self, s: str) -> int:
ans = 1
mod = 10**9 + 7
ans = mul = 1
for w in s.split():
cnt = Counter(w)
ans *= f[len(w)]
ans %= mod
for v in cnt.values():
ans *= pow(f[v], -1, mod)
ans %= mod
return ans
cnt = Counter()
for i, c in enumerate(w, 1):
cnt[c] += 1
mul = mul * cnt[c] % mod
ans = ans * i % mod
return ans * pow(mul, -1, mod) % mod
11 changes: 0 additions & 11 deletions solution/2500-2599/2514.Count Anagrams/Solution2.py

This file was deleted.

Loading