diff --git a/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md b/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md index 42125ff78479b..2bb936fa3ae5b 100644 --- a/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md +++ b/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md @@ -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$. diff --git a/solution/2300-2399/2396.Strictly Palindromic Number/README.md b/solution/2300-2399/2396.Strictly Palindromic Number/README.md index e4d6dc39cd173..f6af6a4353763 100644 --- a/solution/2300-2399/2396.Strictly Palindromic Number/README.md +++ b/solution/2300-2399/2396.Strictly Palindromic Number/README.md @@ -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)$。 diff --git a/solution/2300-2399/2396.Strictly Palindromic Number/README_EN.md b/solution/2300-2399/2396.Strictly Palindromic Number/README_EN.md index ab296963a1184..d274a431f4e64 100644 --- a/solution/2300-2399/2396.Strictly Palindromic Number/README_EN.md +++ b/solution/2300-2399/2396.Strictly Palindromic Number/README_EN.md @@ -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)$. diff --git a/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md b/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md index 35ba40e2f2563..0a8f2c13fe4d4 100644 --- a/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md +++ b/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md @@ -50,7 +50,9 @@ ### 方法一:双指针 + 单调队列 -问题实际上是求滑动窗口内的最大值,可以用单调队列来求解。只需要二分枚举窗口 $k$ 的大小,找到一个最大的 $k$,使得满足题目要求。 +问题实际上是求滑动窗口内的最大值,可以用单调队列来求解。 + +我们只需要二分枚举窗口 $k$ 的大小,找到一个最大的 $k$,使得满足题目要求。 实现过程中,实际上不需要进行二分枚举,只需要将固定窗口改为双指针非固定窗口即可。 diff --git a/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md b/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md index 4ef22892c18bc..94611ca3839ad 100644 --- a/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md +++ b/solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md @@ -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. diff --git a/solution/2400-2499/2417.Closest Fair Integer/README_EN.md b/solution/2400-2499/2417.Closest Fair Integer/README_EN.md index 895fa27f16d8b..ab971f9baf8f7 100644 --- a/solution/2400-2499/2417.Closest Fair Integer/README_EN.md +++ b/solution/2400-2499/2417.Closest Fair Integer/README_EN.md @@ -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)$. diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md index 443ebe53ab860..08656c362ec8d 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md @@ -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$ 是单词的长度。 @@ -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; @@ -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; } } @@ -157,28 +159,45 @@ function twoEditWords(queries: string[], dictionary: string[]): string[] { ```rust impl Solution { pub fn two_edit_words(queries: Vec, dictionary: Vec) -> Vec { - 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 TwoEditWords(string[] queries, string[] dictionary) { + var ans = new List(); + 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; + } +} +``` + diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md index 3d099dc689f2b..e4737a30aa644 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md @@ -47,7 +47,11 @@ Applying any two edits to "yes" cannot make it equal to "not" ## 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. @@ -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; @@ -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; } } @@ -150,28 +156,45 @@ function twoEditWords(queries: string[], dictionary: string[]): string[] { ```rust impl Solution { pub fn two_edit_words(queries: Vec, dictionary: Vec) -> Vec { - 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 TwoEditWords(string[] queries, string[] dictionary) { + var ans = new List(); + 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; + } +} +``` + diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cpp b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cpp index cf0a65df1f7b3..494ef0a9ac23d 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cpp +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cpp @@ -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; diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cs b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cs new file mode 100644 index 0000000000000..fed4869c6d9b0 --- /dev/null +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cs @@ -0,0 +1,20 @@ +public class Solution { + public IList TwoEditWords(string[] queries, string[] dictionary) { + var ans = new List(); + 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; + } +} \ No newline at end of file diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.rs b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.rs index 14a78e8907dda..5af085ccc6be5 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.rs +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.rs @@ -1,21 +1,15 @@ impl Solution { pub fn two_edit_words(queries: Vec, dictionary: Vec) -> Vec { - 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() } diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.ts b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.ts index 6ee99076801fb..97dc0b4d82a6e 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.ts +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.ts @@ -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; } }