Skip to content

feat: add solutions to lc problem: No.2452 #2778

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
May 10, 2024
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 @@ -48,7 +48,13 @@ Note that even though the subarrays have the same content, the two subarrays are

## Solutions

### Solution 1
### Solution 1: Hash Table

We can traverse the array $nums$, and use a hash table $vis$ to record the sum of every two adjacent elements in the array. If the sum of the current two elements has already appeared in the hash table, then return `true`. Otherwise, add the sum of the current two elements to the hash table.

If we finish traversing and haven't found two subarrays that meet the condition, return `false`.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.

<!-- tabs:start -->

Expand Down
6 changes: 3 additions & 3 deletions solution/2300-2399/2396.Strictly Palindromic Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@

### 方法一:脑筋急转弯

当 $n=4$ 时,二进制表示为 $100$,不是回文串;
当 $n = 4$ 时,二进制表示为 $100$,不是回文串;

当 $n \gt 4$ 时,此时 $n-2$ 的二进制表示为 $12$,不是回文串。
当 $n \gt 4$ 时,此时 $n - 2$ 进制表示为 $12$,不是回文串。

因此,我们直接返回 `false` 即可
因此,我们可以直接返回 `false`。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ Therefore, we return false.

## Solutions

### Solution 1
### Solution 1: Quick Thinking

When $n = 4$, its binary representation is $100$, which is not a palindrome;

When $n \gt 4$, its $(n - 2)$-ary representation is $12$, which is not a palindrome.

Therefore, we can directly return `false`.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@

### 方法一:双指针 + 单调队列

问题实际上是求滑动窗口内的最大值,可以用单调队列来求解。只需要二分枚举窗口 $k$ 的大小,找到一个最大的 $k$,使得满足题目要求。
问题实际上是求滑动窗口内的最大值,可以用单调队列来求解。

我们只需要二分枚举窗口 $k$ 的大小,找到一个最大的 $k$,使得满足题目要求。

实现过程中,实际上不需要进行二分枚举,只需要将固定窗口改为双指针非固定窗口即可。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ It can be shown that it is not possible to run more than 3 consecutive robots wi

## Solutions

### Solution 1
### Solution 1: Two Pointers + Monotonic Queue

The problem is essentially finding the maximum value within a sliding window, which can be solved using a monotonic queue.

We only need to use binary search to enumerate the size of the window $k$, and find the largest $k$ that satisfies the problem requirements.

In the implementation process, we don't actually need to perform binary search enumeration. We just need to change the fixed window to a non-fixed window with double pointers.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of robots in the problem.

<!-- tabs:start -->

Expand Down
9 changes: 8 additions & 1 deletion solution/2400-2499/2417.Closest Fair Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@

## Solutions

### Solution 1
### Solution 1: Case Discussion

We denote the number of digits of $n$ as $k$, and the number of odd and even digits as $a$ and $b$ respectively.

- If $a = b$, then $n$ itself is `fair`, and we can directly return $n$;
- Otherwise, if $k$ is odd, we can find the smallest `fair` number with $k+1$ digits, in the form of `10000111`. If $k$ is even, we can directly brute force `closestFair(n+1)`.

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@

### 方法一:暴力枚举

遍历 `queries` 中的每个单词,对于每个单词,遍历 `dictionary` 中的每个单词,判断两个单词不同字符的位置数是否小于 $3$,如果是,则将该单词加入结果集
我们直接遍历数组 $\text{queries}$ 中的每个单词 $s$,再遍历数组 $\text{dictionary}$ 中的每个单词 $t$,如果存在一个单词 $t$ 与 $s$ 的编辑距离小于 $3$,则将 $s$ 加入答案数组中,然后退出内层循环的遍历。如果不存在这样的单词 $t$,则继续遍历下一个单词 $s$

时间复杂度 $O(m\times n\times k)$。其中 $m$ 和 $n$ 分别是 `queries``dictionary` 的长度,而 $k$ 是 `queries` 和 `dictionary` 中单词的长度
时间复杂度 $O(m \times n \times l)$,其中 $m$ 和 $n$ 分别是数组 $\text{queries}$$\text{dictionary}$ 的长度,而 $l$ 是单词的长度

<!-- tabs:start -->

Expand Down Expand Up @@ -102,7 +102,9 @@ public:
for (auto& s : queries) {
for (auto& t : dictionary) {
int cnt = 0;
for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
for (int i = 0; i < s.size(); ++i) {
cnt += s[i] != t[i];
}
if (cnt < 3) {
ans.emplace_back(s);
break;
Expand Down Expand Up @@ -137,15 +139,15 @@ func twoEditWords(queries []string, dictionary []string) (ans []string) {
```ts
function twoEditWords(queries: string[], dictionary: string[]): string[] {
const n = queries[0].length;
return queries.filter(querie => {
for (const s of dictionary) {
return queries.filter(s => {
for (const t of dictionary) {
let diff = 0;
for (let i = 0; i < n; i++) {
if (querie[i] !== s[i] && ++diff > 2) {
break;
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
++diff;
}
}
if (diff <= 2) {
if (diff < 3) {
return true;
}
}
Expand All @@ -157,28 +159,45 @@ function twoEditWords(queries: string[], dictionary: string[]): string[] {
```rust
impl Solution {
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
let n = queries[0].len();
queries
.into_iter()
.filter(|querie| {
for s in dictionary.iter() {
let mut diff = 0;
for i in 0..n {
if querie.as_bytes()[i] != s.as_bytes()[i] {
diff += 1;
}
}
if diff <= 2 {
return true;
}
}
false
.filter(|s| {
dictionary.iter().any(|t| {
s
.chars()
.zip(t.chars())
.filter(|&(a, b)| a != b)
.count() < 3
})
})
.collect()
}
}
```

```cs
public class Solution {
public IList<string> TwoEditWords(string[] queries, string[] dictionary) {
var ans = new List<string>();
foreach (var s in queries) {
foreach (var t in dictionary) {
int cnt = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] != t[i]) {
cnt++;
}
}
if (cnt < 3) {
ans.Add(s);
break;
}
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ Applying any two edits to &quot;yes&quot; cannot make it equal to &quot;not&quot

## Solutions

### Solution 1
### Solution 1: Brute Force Enumeration

We directly traverse each word $s$ in the array $\text{queries}$, and then traverse each word $t$ in the array $\text{dictionary}$. If there exists a word $t$ whose edit distance from $s$ is less than $3$, we add $s$ to the answer array and then exit the inner loop. If there is no such word $t$, we continue to traverse the next word $s$.

The time complexity is $O(m \times n \times l)$, where $m$ and $n$ are the lengths of the arrays $\text{queries}$ and $\text{dictionary}$ respectively, and $l$ is the length of the word.

<!-- tabs:start -->

Expand Down Expand Up @@ -95,7 +99,9 @@ public:
for (auto& s : queries) {
for (auto& t : dictionary) {
int cnt = 0;
for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
for (int i = 0; i < s.size(); ++i) {
cnt += s[i] != t[i];
}
if (cnt < 3) {
ans.emplace_back(s);
break;
Expand Down Expand Up @@ -130,15 +136,15 @@ func twoEditWords(queries []string, dictionary []string) (ans []string) {
```ts
function twoEditWords(queries: string[], dictionary: string[]): string[] {
const n = queries[0].length;
return queries.filter(querie => {
for (const s of dictionary) {
return queries.filter(s => {
for (const t of dictionary) {
let diff = 0;
for (let i = 0; i < n; i++) {
if (querie[i] !== s[i] && ++diff > 2) {
break;
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
++diff;
}
}
if (diff <= 2) {
if (diff < 3) {
return true;
}
}
Expand All @@ -150,28 +156,45 @@ function twoEditWords(queries: string[], dictionary: string[]): string[] {
```rust
impl Solution {
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
let n = queries[0].len();
queries
.into_iter()
.filter(|querie| {
for s in dictionary.iter() {
let mut diff = 0;
for i in 0..n {
if querie.as_bytes()[i] != s.as_bytes()[i] {
diff += 1;
}
}
if diff <= 2 {
return true;
}
}
false
.filter(|s| {
dictionary.iter().any(|t| {
s
.chars()
.zip(t.chars())
.filter(|&(a, b)| a != b)
.count() < 3
})
})
.collect()
}
}
```

```cs
public class Solution {
public IList<string> TwoEditWords(string[] queries, string[] dictionary) {
var ans = new List<string>();
foreach (var s in queries) {
foreach (var t in dictionary) {
int cnt = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] != t[i]) {
cnt++;
}
}
if (cnt < 3) {
ans.Add(s);
break;
}
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Solution {
for (auto& s : queries) {
for (auto& t : dictionary) {
int cnt = 0;
for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
for (int i = 0; i < s.size(); ++i) {
cnt += s[i] != t[i];
}
if (cnt < 3) {
ans.emplace_back(s);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Solution {
public IList<string> TwoEditWords(string[] queries, string[] dictionary) {
var ans = new List<string>();
foreach (var s in queries) {
foreach (var t in dictionary) {
int cnt = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] != t[i]) {
cnt++;
}
}
if (cnt < 3) {
ans.Add(s);
break;
}
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
impl Solution {
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
let n = queries[0].len();
queries
.into_iter()
.filter(|querie| {
for s in dictionary.iter() {
let mut diff = 0;
for i in 0..n {
if querie.as_bytes()[i] != s.as_bytes()[i] {
diff += 1;
}
}
if diff <= 2 {
return true;
}
}
false
.filter(|s| {
dictionary.iter().any(|t| {
s
.chars()
.zip(t.chars())
.filter(|&(a, b)| a != b)
.count() < 3
})
})
.collect()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function twoEditWords(queries: string[], dictionary: string[]): string[] {
const n = queries[0].length;
return queries.filter(querie => {
for (const s of dictionary) {
return queries.filter(s => {
for (const t of dictionary) {
let diff = 0;
for (let i = 0; i < n; i++) {
if (querie[i] !== s[i] && ++diff > 2) {
break;
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
++diff;
}
}
if (diff <= 2) {
if (diff < 3) {
return true;
}
}
Expand Down