diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md b/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md index 5d9bfce5f8868..66ddc9faa66c4 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md @@ -100,25 +100,21 @@ class Solution: k = len(words[0]) ans = [] for i in range(k): - cnt1 = Counter() l = r = i - t = 0 + cnt1 = Counter() while r + k <= m: - w = s[r : r + k] + t = s[r : r + k] r += k - if w not in cnt: + if cnt[t] == 0: l = r cnt1.clear() - t = 0 continue - cnt1[w] += 1 - t += 1 - while cnt1[w] > cnt[w]: - remove = s[l : l + k] + cnt1[t] += 1 + while cnt1[t] > cnt[t]: + rem = s[l : l + k] l += k - cnt1[remove] -= 1 - t -= 1 - if t == n: + cnt1[rem] -= 1 + if r - l == n * k: ans.append(l) return ans ``` @@ -129,34 +125,31 @@ class Solution: class Solution { public List findSubstring(String s, String[] words) { Map cnt = new HashMap<>(); - for (String w : words) { + for (var w : words) { cnt.merge(w, 1, Integer::sum); } - int m = s.length(), n = words.length; - int k = words[0].length(); List ans = new ArrayList<>(); + int m = s.length(), n = words.length, k = words[0].length(); for (int i = 0; i < k; ++i) { - Map cnt1 = new HashMap<>(); int l = i, r = i; - int t = 0; + Map cnt1 = new HashMap<>(); while (r + k <= m) { - String w = s.substring(r, r + k); + var t = s.substring(r, r + k); r += k; - if (!cnt.containsKey(w)) { + if (!cnt.containsKey(t)) { cnt1.clear(); l = r; - t = 0; continue; } - cnt1.merge(w, 1, Integer::sum); - ++t; - while (cnt1.get(w) > cnt.get(w)) { - String remove = s.substring(l, l + k); + cnt1.merge(t, 1, Integer::sum); + while (cnt1.get(t) > cnt.get(t)) { + String w = s.substring(l, l + k); + if (cnt1.merge(w, -1, Integer::sum) == 0) { + cnt1.remove(w); + } l += k; - cnt1.merge(remove, -1, Integer::sum); - --t; } - if (t == n) { + if (r - l == n * k) { ans.add(l); } } @@ -173,37 +166,42 @@ class Solution { public: vector findSubstring(string s, vector& words) { unordered_map cnt; - for (auto& w : words) { - ++cnt[w]; + for (const auto& w : words) { + cnt[w]++; } - int m = s.size(), n = words.size(), k = words[0].size(); + vector ans; + int m = s.length(), n = words.size(), k = words[0].length(); + for (int i = 0; i < k; ++i) { - unordered_map cnt1; int l = i, r = i; - int t = 0; + unordered_map cnt1; while (r + k <= m) { - string w = s.substr(r, k); + string t = s.substr(r, k); r += k; - if (!cnt.count(w)) { + + if (!cnt.contains(t)) { cnt1.clear(); l = r; - t = 0; continue; } - ++cnt1[w]; - ++t; - while (cnt1[w] > cnt[w]) { - string remove = s.substr(l, k); + + cnt1[t]++; + + while (cnt1[t] > cnt[t]) { + string w = s.substr(l, k); + if (--cnt1[w] == 0) { + cnt1.erase(w); + } l += k; - --cnt1[remove]; - --t; } - if (t == n) { + + if (r - l == n * k) { ans.push_back(l); } } } + return ans; } }; @@ -213,30 +211,33 @@ public: ```go func findSubstring(s string, words []string) (ans []int) { - cnt := map[string]int{} + cnt := make(map[string]int) for _, w := range words { cnt[w]++ } m, n, k := len(s), len(words), len(words[0]) for i := 0; i < k; i++ { - cnt1 := map[string]int{} - l, r, t := i, i, 0 + l, r := i, i + cnt1 := make(map[string]int) for r+k <= m { - w := s[r : r+k] + t := s[r : r+k] r += k - if _, ok := cnt[w]; !ok { - l, t = r, 0 - cnt1 = map[string]int{} + + if _, exists := cnt[t]; !exists { + cnt1 = make(map[string]int) + l = r continue } - cnt1[w]++ - t++ - for cnt1[w] > cnt[w] { - cnt1[s[l:l+k]]-- + cnt1[t]++ + for cnt1[t] > cnt[t] { + w := s[l : l+k] + cnt1[w]-- + if cnt1[w] == 0 { + delete(cnt1, w) + } l += k - t-- } - if t == n { + if r-l == n*k { ans = append(ans, l) } } @@ -253,33 +254,29 @@ function findSubstring(s: string, words: string[]): number[] { for (const w of words) { cnt.set(w, (cnt.get(w) || 0) + 1); } - const m = s.length; - const n = words.length; - const k = words[0].length; const ans: number[] = []; - for (let i = 0; i < k; ++i) { + const [m, n, k] = [s.length, words.length, words[0].length]; + for (let i = 0; i < k; i++) { + let [l, r] = [i, i]; const cnt1: Map = new Map(); - let l = i; - let r = i; - let t = 0; while (r + k <= m) { - const w = s.slice(r, r + k); + const t = s.substring(r, r + k); r += k; - if (!cnt.has(w)) { + if (!cnt.has(t)) { cnt1.clear(); l = r; - t = 0; continue; } - cnt1.set(w, (cnt1.get(w) || 0) + 1); - ++t; - while (cnt1.get(w)! - cnt.get(w)! > 0) { - const remove = s.slice(l, l + k); - cnt1.set(remove, cnt1.get(remove)! - 1); + cnt1.set(t, (cnt1.get(t) || 0) + 1); + while (cnt1.get(t)! > cnt.get(t)!) { + const w = s.substring(l, l + k); + cnt1.set(w, cnt1.get(w)! - 1); + if (cnt1.get(w) === 0) { + cnt1.delete(w); + } l += k; - --t; } - if (t === n) { + if (r - l === n * k) { ans.push(l); } } @@ -295,40 +292,50 @@ public class Solution { public IList FindSubstring(string s, string[] words) { var cnt = new Dictionary(); foreach (var w in words) { - if (!cnt.ContainsKey(w)) { - cnt[w] = 0; + if (cnt.ContainsKey(w)) { + cnt[w]++; + } else { + cnt[w] = 1; } - ++cnt[w]; } - int m = s.Length, n = words.Length, k = words[0].Length; + var ans = new List(); + int m = s.Length, n = words.Length, k = words[0].Length; + for (int i = 0; i < k; ++i) { + int l = i, r = i; var cnt1 = new Dictionary(); - int l = i, r = i, t = 0; while (r + k <= m) { - var w = s.Substring(r, k); + var t = s.Substring(r, k); r += k; - if (!cnt.ContainsKey(w)) { + + if (!cnt.ContainsKey(t)) { cnt1.Clear(); - t = 0; l = r; continue; } - if (!cnt1.ContainsKey(w)) { - cnt1[w] = 0; + + if (cnt1.ContainsKey(t)) { + cnt1[t]++; + } else { + cnt1[t] = 1; } - ++cnt1[w]; - ++t; - while (cnt1[w] > cnt[w]) { - --cnt1[s.Substring(l, k)]; + + while (cnt1[t] > cnt[t]) { + var w = s.Substring(l, k); + cnt1[w]--; + if (cnt1[w] == 0) { + cnt1.Remove(w); + } l += k; - --t; } - if (t == n) { + + if (r - l == n * k) { ans.Add(l); } } } + return ans; } } @@ -346,47 +353,53 @@ class Solution { function findSubstring($s, $words) { $cnt = []; foreach ($words as $w) { - if (!isset($cnt[$w])) { - $cnt[$w] = 1; - } else { + if (isset($cnt[$w])) { $cnt[$w]++; + } else { + $cnt[$w] = 1; } } + + $ans = []; $m = strlen($s); $n = count($words); $k = strlen($words[0]); - $ans = []; - for ($i = 0; $i < $k; ++$i) { - $cnt1 = []; + + for ($i = 0; $i < $k; $i++) { $l = $i; $r = $i; - $t = 0; + $cnt1 = []; while ($r + $k <= $m) { - $w = substr($s, $r, $k); + $t = substr($s, $r, $k); $r += $k; - if (!array_key_exists($w, $cnt)) { + + if (!isset($cnt[$t])) { $cnt1 = []; $l = $r; - $t = 0; continue; } - if (!isset($cnt1[$w])) { - $cnt1[$w] = 1; + + if (isset($cnt1[$t])) { + $cnt1[$t]++; } else { - $cnt1[$w]++; + $cnt1[$t] = 1; } - ++$t; - while ($cnt1[$w] > $cnt[$w]) { - $remove = substr($s, $l, $k); + + while ($cnt1[$t] > $cnt[$t]) { + $w = substr($s, $l, $k); + $cnt1[$w]--; + if ($cnt1[$w] == 0) { + unset($cnt1[$w]); + } $l += $k; - $cnt1[$remove]--; - $t--; } - if ($t == $n) { + + if ($r - $l == $n * $k) { $ans[] = $l; } } } + return $ans; } } @@ -396,44 +409,4 @@ class Solution { - - -### 方法二 - - - -#### C++ - -```cpp -class Solution { -public: - vector findSubstring(string s, vector& words) { - unordered_map d; - for (auto& w : words) ++d[w]; - vector ans; - int n = s.size(), m = words.size(), k = words[0].size(); - for (int i = 0; i < k; ++i) { - int cnt = 0; - unordered_map t; - for (int j = i; j <= n; j += k) { - if (j - i >= m * k) { - auto s1 = s.substr(j - m * k, k); - --t[s1]; - cnt -= d[s1] > t[s1]; - } - auto s2 = s.substr(j, k); - ++t[s2]; - cnt += d[s2] >= t[s2]; - if (cnt == m) ans.emplace_back(j - (m - 1) * k); - } - } - return ans; - } -}; -``` - - - - - diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md b/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md index 02dffbb52991e..3f410f7e61eda 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md @@ -106,25 +106,21 @@ class Solution: k = len(words[0]) ans = [] for i in range(k): - cnt1 = Counter() l = r = i - t = 0 + cnt1 = Counter() while r + k <= m: - w = s[r : r + k] + t = s[r : r + k] r += k - if w not in cnt: + if cnt[t] == 0: l = r cnt1.clear() - t = 0 continue - cnt1[w] += 1 - t += 1 - while cnt1[w] > cnt[w]: - remove = s[l : l + k] + cnt1[t] += 1 + while cnt1[t] > cnt[t]: + rem = s[l : l + k] l += k - cnt1[remove] -= 1 - t -= 1 - if t == n: + cnt1[rem] -= 1 + if r - l == n * k: ans.append(l) return ans ``` @@ -135,34 +131,31 @@ class Solution: class Solution { public List findSubstring(String s, String[] words) { Map cnt = new HashMap<>(); - for (String w : words) { + for (var w : words) { cnt.merge(w, 1, Integer::sum); } - int m = s.length(), n = words.length; - int k = words[0].length(); List ans = new ArrayList<>(); + int m = s.length(), n = words.length, k = words[0].length(); for (int i = 0; i < k; ++i) { - Map cnt1 = new HashMap<>(); int l = i, r = i; - int t = 0; + Map cnt1 = new HashMap<>(); while (r + k <= m) { - String w = s.substring(r, r + k); + var t = s.substring(r, r + k); r += k; - if (!cnt.containsKey(w)) { + if (!cnt.containsKey(t)) { cnt1.clear(); l = r; - t = 0; continue; } - cnt1.merge(w, 1, Integer::sum); - ++t; - while (cnt1.get(w) > cnt.get(w)) { - String remove = s.substring(l, l + k); + cnt1.merge(t, 1, Integer::sum); + while (cnt1.get(t) > cnt.get(t)) { + String w = s.substring(l, l + k); + if (cnt1.merge(w, -1, Integer::sum) == 0) { + cnt1.remove(w); + } l += k; - cnt1.merge(remove, -1, Integer::sum); - --t; } - if (t == n) { + if (r - l == n * k) { ans.add(l); } } @@ -179,37 +172,42 @@ class Solution { public: vector findSubstring(string s, vector& words) { unordered_map cnt; - for (auto& w : words) { - ++cnt[w]; + for (const auto& w : words) { + cnt[w]++; } - int m = s.size(), n = words.size(), k = words[0].size(); + vector ans; + int m = s.length(), n = words.size(), k = words[0].length(); + for (int i = 0; i < k; ++i) { - unordered_map cnt1; int l = i, r = i; - int t = 0; + unordered_map cnt1; while (r + k <= m) { - string w = s.substr(r, k); + string t = s.substr(r, k); r += k; - if (!cnt.count(w)) { + + if (!cnt.contains(t)) { cnt1.clear(); l = r; - t = 0; continue; } - ++cnt1[w]; - ++t; - while (cnt1[w] > cnt[w]) { - string remove = s.substr(l, k); + + cnt1[t]++; + + while (cnt1[t] > cnt[t]) { + string w = s.substr(l, k); + if (--cnt1[w] == 0) { + cnt1.erase(w); + } l += k; - --cnt1[remove]; - --t; } - if (t == n) { + + if (r - l == n * k) { ans.push_back(l); } } } + return ans; } }; @@ -219,30 +217,33 @@ public: ```go func findSubstring(s string, words []string) (ans []int) { - cnt := map[string]int{} + cnt := make(map[string]int) for _, w := range words { cnt[w]++ } m, n, k := len(s), len(words), len(words[0]) for i := 0; i < k; i++ { - cnt1 := map[string]int{} - l, r, t := i, i, 0 + l, r := i, i + cnt1 := make(map[string]int) for r+k <= m { - w := s[r : r+k] + t := s[r : r+k] r += k - if _, ok := cnt[w]; !ok { - l, t = r, 0 - cnt1 = map[string]int{} + + if _, exists := cnt[t]; !exists { + cnt1 = make(map[string]int) + l = r continue } - cnt1[w]++ - t++ - for cnt1[w] > cnt[w] { - cnt1[s[l:l+k]]-- + cnt1[t]++ + for cnt1[t] > cnt[t] { + w := s[l : l+k] + cnt1[w]-- + if cnt1[w] == 0 { + delete(cnt1, w) + } l += k - t-- } - if t == n { + if r-l == n*k { ans = append(ans, l) } } @@ -259,33 +260,29 @@ function findSubstring(s: string, words: string[]): number[] { for (const w of words) { cnt.set(w, (cnt.get(w) || 0) + 1); } - const m = s.length; - const n = words.length; - const k = words[0].length; const ans: number[] = []; - for (let i = 0; i < k; ++i) { + const [m, n, k] = [s.length, words.length, words[0].length]; + for (let i = 0; i < k; i++) { + let [l, r] = [i, i]; const cnt1: Map = new Map(); - let l = i; - let r = i; - let t = 0; while (r + k <= m) { - const w = s.slice(r, r + k); + const t = s.substring(r, r + k); r += k; - if (!cnt.has(w)) { + if (!cnt.has(t)) { cnt1.clear(); l = r; - t = 0; continue; } - cnt1.set(w, (cnt1.get(w) || 0) + 1); - ++t; - while (cnt1.get(w)! - cnt.get(w)! > 0) { - const remove = s.slice(l, l + k); - cnt1.set(remove, cnt1.get(remove)! - 1); + cnt1.set(t, (cnt1.get(t) || 0) + 1); + while (cnt1.get(t)! > cnt.get(t)!) { + const w = s.substring(l, l + k); + cnt1.set(w, cnt1.get(w)! - 1); + if (cnt1.get(w) === 0) { + cnt1.delete(w); + } l += k; - --t; } - if (t === n) { + if (r - l === n * k) { ans.push(l); } } @@ -301,40 +298,50 @@ public class Solution { public IList FindSubstring(string s, string[] words) { var cnt = new Dictionary(); foreach (var w in words) { - if (!cnt.ContainsKey(w)) { - cnt[w] = 0; + if (cnt.ContainsKey(w)) { + cnt[w]++; + } else { + cnt[w] = 1; } - ++cnt[w]; } - int m = s.Length, n = words.Length, k = words[0].Length; + var ans = new List(); + int m = s.Length, n = words.Length, k = words[0].Length; + for (int i = 0; i < k; ++i) { + int l = i, r = i; var cnt1 = new Dictionary(); - int l = i, r = i, t = 0; while (r + k <= m) { - var w = s.Substring(r, k); + var t = s.Substring(r, k); r += k; - if (!cnt.ContainsKey(w)) { + + if (!cnt.ContainsKey(t)) { cnt1.Clear(); - t = 0; l = r; continue; } - if (!cnt1.ContainsKey(w)) { - cnt1[w] = 0; + + if (cnt1.ContainsKey(t)) { + cnt1[t]++; + } else { + cnt1[t] = 1; } - ++cnt1[w]; - ++t; - while (cnt1[w] > cnt[w]) { - --cnt1[s.Substring(l, k)]; + + while (cnt1[t] > cnt[t]) { + var w = s.Substring(l, k); + cnt1[w]--; + if (cnt1[w] == 0) { + cnt1.Remove(w); + } l += k; - --t; } - if (t == n) { + + if (r - l == n * k) { ans.Add(l); } } } + return ans; } } @@ -352,47 +359,53 @@ class Solution { function findSubstring($s, $words) { $cnt = []; foreach ($words as $w) { - if (!isset($cnt[$w])) { - $cnt[$w] = 1; - } else { + if (isset($cnt[$w])) { $cnt[$w]++; + } else { + $cnt[$w] = 1; } } + + $ans = []; $m = strlen($s); $n = count($words); $k = strlen($words[0]); - $ans = []; - for ($i = 0; $i < $k; ++$i) { - $cnt1 = []; + + for ($i = 0; $i < $k; $i++) { $l = $i; $r = $i; - $t = 0; + $cnt1 = []; while ($r + $k <= $m) { - $w = substr($s, $r, $k); + $t = substr($s, $r, $k); $r += $k; - if (!array_key_exists($w, $cnt)) { + + if (!isset($cnt[$t])) { $cnt1 = []; $l = $r; - $t = 0; continue; } - if (!isset($cnt1[$w])) { - $cnt1[$w] = 1; + + if (isset($cnt1[$t])) { + $cnt1[$t]++; } else { - $cnt1[$w]++; + $cnt1[$t] = 1; } - ++$t; - while ($cnt1[$w] > $cnt[$w]) { - $remove = substr($s, $l, $k); + + while ($cnt1[$t] > $cnt[$t]) { + $w = substr($s, $l, $k); + $cnt1[$w]--; + if ($cnt1[$w] == 0) { + unset($cnt1[$w]); + } $l += $k; - $cnt1[$remove]--; - $t--; } - if ($t == $n) { + + if ($r - $l == $n * $k) { $ans[] = $l; } } } + return $ans; } } @@ -402,44 +415,4 @@ class Solution { - - -### Solution 2 - - - -#### C++ - -```cpp -class Solution { -public: - vector findSubstring(string s, vector& words) { - unordered_map d; - for (auto& w : words) ++d[w]; - vector ans; - int n = s.size(), m = words.size(), k = words[0].size(); - for (int i = 0; i < k; ++i) { - int cnt = 0; - unordered_map t; - for (int j = i; j <= n; j += k) { - if (j - i >= m * k) { - auto s1 = s.substr(j - m * k, k); - --t[s1]; - cnt -= d[s1] > t[s1]; - } - auto s2 = s.substr(j, k); - ++t[s2]; - cnt += d[s2] >= t[s2]; - if (cnt == m) ans.emplace_back(j - (m - 1) * k); - } - } - return ans; - } -}; -``` - - - - - diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp index abe7c53d28ef5..bcadd9565442d 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp @@ -2,37 +2,42 @@ class Solution { public: vector findSubstring(string s, vector& words) { unordered_map cnt; - for (auto& w : words) { - ++cnt[w]; + for (const auto& w : words) { + cnt[w]++; } - int m = s.size(), n = words.size(), k = words[0].size(); + vector ans; + int m = s.length(), n = words.size(), k = words[0].length(); + for (int i = 0; i < k; ++i) { - unordered_map cnt1; int l = i, r = i; - int t = 0; + unordered_map cnt1; while (r + k <= m) { - string w = s.substr(r, k); + string t = s.substr(r, k); r += k; - if (!cnt.count(w)) { + + if (!cnt.contains(t)) { cnt1.clear(); l = r; - t = 0; continue; } - ++cnt1[w]; - ++t; - while (cnt1[w] > cnt[w]) { - string remove = s.substr(l, k); + + cnt1[t]++; + + while (cnt1[t] > cnt[t]) { + string w = s.substr(l, k); + if (--cnt1[w] == 0) { + cnt1.erase(w); + } l += k; - --cnt1[remove]; - --t; } - if (t == n) { + + if (r - l == n * k) { ans.push_back(l); } } } + return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cs b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cs index e631e724cf196..ec0eb534b2344 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cs +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cs @@ -2,40 +2,50 @@ public class Solution { public IList FindSubstring(string s, string[] words) { var cnt = new Dictionary(); foreach (var w in words) { - if (!cnt.ContainsKey(w)) { - cnt[w] = 0; + if (cnt.ContainsKey(w)) { + cnt[w]++; + } else { + cnt[w] = 1; } - ++cnt[w]; } - int m = s.Length, n = words.Length, k = words[0].Length; + var ans = new List(); + int m = s.Length, n = words.Length, k = words[0].Length; + for (int i = 0; i < k; ++i) { + int l = i, r = i; var cnt1 = new Dictionary(); - int l = i, r = i, t = 0; while (r + k <= m) { - var w = s.Substring(r, k); + var t = s.Substring(r, k); r += k; - if (!cnt.ContainsKey(w)) { + + if (!cnt.ContainsKey(t)) { cnt1.Clear(); - t = 0; l = r; continue; } - if (!cnt1.ContainsKey(w)) { - cnt1[w] = 0; + + if (cnt1.ContainsKey(t)) { + cnt1[t]++; + } else { + cnt1[t] = 1; } - ++cnt1[w]; - ++t; - while (cnt1[w] > cnt[w]) { - --cnt1[s.Substring(l, k)]; + + while (cnt1[t] > cnt[t]) { + var w = s.Substring(l, k); + cnt1[w]--; + if (cnt1[w] == 0) { + cnt1.Remove(w); + } l += k; - --t; } - if (t == n) { + + if (r - l == n * k) { ans.Add(l); } } } + return ans; } } diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.go b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.go index 21ff0def7e273..81cd58b695518 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.go +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.go @@ -1,31 +1,34 @@ func findSubstring(s string, words []string) (ans []int) { - cnt := map[string]int{} + cnt := make(map[string]int) for _, w := range words { cnt[w]++ } m, n, k := len(s), len(words), len(words[0]) for i := 0; i < k; i++ { - cnt1 := map[string]int{} - l, r, t := i, i, 0 + l, r := i, i + cnt1 := make(map[string]int) for r+k <= m { - w := s[r : r+k] + t := s[r : r+k] r += k - if _, ok := cnt[w]; !ok { - l, t = r, 0 - cnt1 = map[string]int{} + + if _, exists := cnt[t]; !exists { + cnt1 = make(map[string]int) + l = r continue } - cnt1[w]++ - t++ - for cnt1[w] > cnt[w] { - cnt1[s[l:l+k]]-- + cnt1[t]++ + for cnt1[t] > cnt[t] { + w := s[l : l+k] + cnt1[w]-- + if cnt1[w] == 0 { + delete(cnt1, w) + } l += k - t-- } - if t == n { + if r-l == n*k { ans = append(ans, l) } } } return -} \ No newline at end of file +} diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.java b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.java index b0c366a84d982..98f5b91328122 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.java +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.java @@ -1,38 +1,35 @@ class Solution { public List findSubstring(String s, String[] words) { Map cnt = new HashMap<>(); - for (String w : words) { + for (var w : words) { cnt.merge(w, 1, Integer::sum); } - int m = s.length(), n = words.length; - int k = words[0].length(); List ans = new ArrayList<>(); + int m = s.length(), n = words.length, k = words[0].length(); for (int i = 0; i < k; ++i) { - Map cnt1 = new HashMap<>(); int l = i, r = i; - int t = 0; + Map cnt1 = new HashMap<>(); while (r + k <= m) { - String w = s.substring(r, r + k); + var t = s.substring(r, r + k); r += k; - if (!cnt.containsKey(w)) { + if (!cnt.containsKey(t)) { cnt1.clear(); l = r; - t = 0; continue; } - cnt1.merge(w, 1, Integer::sum); - ++t; - while (cnt1.get(w) > cnt.get(w)) { - String remove = s.substring(l, l + k); + cnt1.merge(t, 1, Integer::sum); + while (cnt1.get(t) > cnt.get(t)) { + String w = s.substring(l, l + k); + if (cnt1.merge(w, -1, Integer::sum) == 0) { + cnt1.remove(w); + } l += k; - cnt1.merge(remove, -1, Integer::sum); - --t; } - if (t == n) { + if (r - l == n * k) { ans.add(l); } } } return ans; } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php index 666c296650ef1..db668e5e1e50a 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php @@ -1,4 +1,5 @@ class Solution { + /** * @param String $s * @param String[] $words @@ -7,47 +8,53 @@ class Solution { function findSubstring($s, $words) { $cnt = []; foreach ($words as $w) { - if (!isset($cnt[$w])) { - $cnt[$w] = 1; - } else { + if (isset($cnt[$w])) { $cnt[$w]++; + } else { + $cnt[$w] = 1; } } + + $ans = []; $m = strlen($s); $n = count($words); $k = strlen($words[0]); - $ans = []; - for ($i = 0; $i < $k; ++$i) { - $cnt1 = []; + + for ($i = 0; $i < $k; $i++) { $l = $i; $r = $i; - $t = 0; + $cnt1 = []; while ($r + $k <= $m) { - $w = substr($s, $r, $k); + $t = substr($s, $r, $k); $r += $k; - if (!array_key_exists($w, $cnt)) { + + if (!isset($cnt[$t])) { $cnt1 = []; $l = $r; - $t = 0; continue; } - if (!isset($cnt1[$w])) { - $cnt1[$w] = 1; + + if (isset($cnt1[$t])) { + $cnt1[$t]++; } else { - $cnt1[$w]++; + $cnt1[$t] = 1; } - ++$t; - while ($cnt1[$w] > $cnt[$w]) { - $remove = substr($s, $l, $k); + + while ($cnt1[$t] > $cnt[$t]) { + $w = substr($s, $l, $k); + $cnt1[$w]--; + if ($cnt1[$w] == 0) { + unset($cnt1[$w]); + } $l += $k; - $cnt1[$remove]--; - $t--; } - if ($t == $n) { + + if ($r - $l == $n * $k) { $ans[] = $l; } } } + return $ans; } } diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.py b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.py index e40d0de4ced8e..1a8cdfd50d6fe 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.py +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.py @@ -5,24 +5,20 @@ def findSubstring(self, s: str, words: List[str]) -> List[int]: k = len(words[0]) ans = [] for i in range(k): - cnt1 = Counter() l = r = i - t = 0 + cnt1 = Counter() while r + k <= m: - w = s[r : r + k] + t = s[r : r + k] r += k - if w not in cnt: + if cnt[t] == 0: l = r cnt1.clear() - t = 0 continue - cnt1[w] += 1 - t += 1 - while cnt1[w] > cnt[w]: - remove = s[l : l + k] + cnt1[t] += 1 + while cnt1[t] > cnt[t]: + rem = s[l : l + k] l += k - cnt1[remove] -= 1 - t -= 1 - if t == n: + cnt1[rem] -= 1 + if r - l == n * k: ans.append(l) return ans diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.ts b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.ts index 01ff17558a90c..8e7d3ccc0527a 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.ts +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.ts @@ -3,33 +3,29 @@ function findSubstring(s: string, words: string[]): number[] { for (const w of words) { cnt.set(w, (cnt.get(w) || 0) + 1); } - const m = s.length; - const n = words.length; - const k = words[0].length; const ans: number[] = []; - for (let i = 0; i < k; ++i) { + const [m, n, k] = [s.length, words.length, words[0].length]; + for (let i = 0; i < k; i++) { + let [l, r] = [i, i]; const cnt1: Map = new Map(); - let l = i; - let r = i; - let t = 0; while (r + k <= m) { - const w = s.slice(r, r + k); + const t = s.substring(r, r + k); r += k; - if (!cnt.has(w)) { + if (!cnt.has(t)) { cnt1.clear(); l = r; - t = 0; continue; } - cnt1.set(w, (cnt1.get(w) || 0) + 1); - ++t; - while (cnt1.get(w)! - cnt.get(w)! > 0) { - const remove = s.slice(l, l + k); - cnt1.set(remove, cnt1.get(remove)! - 1); + cnt1.set(t, (cnt1.get(t) || 0) + 1); + while (cnt1.get(t)! > cnt.get(t)!) { + const w = s.substring(l, l + k); + cnt1.set(w, cnt1.get(w)! - 1); + if (cnt1.get(w) === 0) { + cnt1.delete(w); + } l += k; - --t; } - if (t === n) { + if (r - l === n * k) { ans.push(l); } } diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution2.cpp b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution2.cpp deleted file mode 100644 index 2e88ae3ac098f..0000000000000 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution2.cpp +++ /dev/null @@ -1,25 +0,0 @@ -class Solution { -public: - vector findSubstring(string s, vector& words) { - unordered_map d; - for (auto& w : words) ++d[w]; - vector ans; - int n = s.size(), m = words.size(), k = words[0].size(); - for (int i = 0; i < k; ++i) { - int cnt = 0; - unordered_map t; - for (int j = i; j <= n; j += k) { - if (j - i >= m * k) { - auto s1 = s.substr(j - m * k, k); - --t[s1]; - cnt -= d[s1] > t[s1]; - } - auto s2 = s.substr(j, k); - ++t[s2]; - cnt += d[s2] >= t[s2]; - if (cnt == m) ans.emplace_back(j - (m - 1) * k); - } - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/0400-0499/0480.Sliding Window Median/README_EN.md b/solution/0400-0499/0480.Sliding Window Median/README_EN.md index e1e9384fd8a51..7dcbf3e826a65 100644 --- a/solution/0400-0499/0480.Sliding Window Median/README_EN.md +++ b/solution/0400-0499/0480.Sliding Window Median/README_EN.md @@ -36,7 +36,7 @@ tags:
 Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
 Output: [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
-Explanation:
+Explanation: 
 Window position                Median
 ---------------                -----
 [1  3  -1] -3  5  3  6  7        1
diff --git a/solution/0500-0599/0588.Design In-Memory File System/README.md b/solution/0500-0599/0588.Design In-Memory File System/README.md
index b543fc76a3ce8..357f38fcca065 100644
--- a/solution/0500-0599/0588.Design In-Memory File System/README.md	
+++ b/solution/0500-0599/0588.Design In-Memory File System/README.md	
@@ -77,6 +77,7 @@ fileSystem.readContentFromFile("/a/b/c/d"); // 返回 "hello"
  • path 和 filePath 都是绝对路径,除非是根目录 ‘/’ 自身,其他路径都是以 ‘/’ 开头且 以 ‘/’ 结束。
  • 你可以假定所有操作的参数都是有效的,即用户不会获取不存在文件的内容,或者获取不存在文件夹和文件的列表。
  • 你可以假定所有文件夹名字和文件名字都只包含小写字母,且同一文件夹下不会有相同名字的文件夹或文件。
  • +
  • 你可以假定 addContentToFile 中的文件的父目录都存在。
  • 1 <= content.length <= 50
  • lsmkdiraddContentToFile, and readContentFromFile 最多被调用 300 次
  • diff --git a/solution/0700-0799/0731.My Calendar II/README_EN.md b/solution/0700-0799/0731.My Calendar II/README_EN.md index 0d5beb8e8da83..030f6b1a38621 100644 --- a/solution/0700-0799/0731.My Calendar II/README_EN.md +++ b/solution/0700-0799/0731.My Calendar II/README_EN.md @@ -46,9 +46,9 @@ tags: Explanation MyCalendarTwo myCalendarTwo = new MyCalendarTwo(); -myCalendarTwo.book(10, 20); // return True, The event can be booked. -myCalendarTwo.book(50, 60); // return True, The event can be booked. -myCalendarTwo.book(10, 40); // return True, The event can be double booked. +myCalendarTwo.book(10, 20); // return True, The event can be booked. +myCalendarTwo.book(50, 60); // return True, The event can be booked. +myCalendarTwo.book(10, 40); // return True, The event can be double booked. myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking. myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked. myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event. diff --git a/solution/0900-0999/0916.Word Subsets/README.md b/solution/0900-0999/0916.Word Subsets/README.md index 551a6413d4ffc..97f9454475748 100644 --- a/solution/0900-0999/0916.Word Subsets/README.md +++ b/solution/0900-0999/0916.Word Subsets/README.md @@ -28,47 +28,36 @@ tags:

    如果对 words2 中的每一个单词 bb 都是 a 的子集,那么我们称 words1 中的单词 a 通用单词

    -

    以数组形式返回 words1 中所有的通用单词。你可以按 任意顺序 返回答案。

    +

    以数组形式返回 words1 中所有的 通用 单词。你可以按 任意顺序 返回答案。

     

    -

    示例 1:

    +

    示例 1:

    -
    -输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
    -输出:["facebook","google","leetcode"]
    -
    +
    +

    输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]

    -

    示例 2:

    +

    输出:["facebook","google","leetcode"]

    +
    -
    -输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
    -输出:["apple","google","leetcode"]
    -
    +

    示例 2:

    -

    示例 3:

    +
    +

    输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["lc","eo"]

    -
    -输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","oo"]
    -输出:["facebook","google"]
    -
    +

    输出:["leetcode"]

    +
    -

    示例 4:

    +

    示例 3:

    -
    -输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["lo","eo"]
    -输出:["google","leetcode"]
    -
    +
    +

    输入:words1 = ["acaac","cccbb","aacbb","caacc","bcbbb"], words2 = ["c","cc","b"]

    -

    示例 5:

    - -
    -输入:words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["ec","oc","ceo"]
    -输出:["facebook","leetcode"]
    -
    +

    输出:["cccbb"]

    +

     

    diff --git a/solution/0900-0999/0975.Odd Even Jump/README_EN.md b/solution/0900-0999/0975.Odd Even Jump/README_EN.md index f3d8f0f75c976..99f76a4a016e2 100644 --- a/solution/0900-0999/0975.Odd Even Jump/README_EN.md +++ b/solution/0900-0999/0975.Odd Even Jump/README_EN.md @@ -40,7 +40,7 @@ tags:
     Input: arr = [10,13,12,14,15]
     Output: 2
    -Explanation:
    +Explanation: 
     From starting index i = 0, we can make our 1st jump to i = 2 (since arr[2] is the smallest among arr[1], arr[2], arr[3], arr[4] that is greater or equal to arr[0]), then we cannot jump any more.
     From starting index i = 1 and i = 2, we can make our 1st jump to i = 3, then we cannot jump any more.
     From starting index i = 3, we can make our 1st jump to i = 4, so we have reached the end.
    @@ -54,7 +54,7 @@ jumps.
     
     Input: arr = [2,3,1,1,4]
     Output: 3
    -Explanation:
    +Explanation: 
     From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
     During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
     During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3
    diff --git a/solution/1100-1199/1172.Dinner Plate Stacks/README.md b/solution/1100-1199/1172.Dinner Plate Stacks/README.md
    index ced8ea8849c43..dcc64da0f7884 100644
    --- a/solution/1100-1199/1172.Dinner Plate Stacks/README.md	
    +++ b/solution/1100-1199/1172.Dinner Plate Stacks/README.md	
    @@ -65,14 +65,14 @@ D.popAtStack(0);   // 返回 20。栈的现状为:       4 21
                                                 ﹈ ﹈ ﹈
     D.popAtStack(2);   // 返回 21。栈的现状为:       4
                                                 1  3  5
    -                                            ﹈ ﹈ ﹈
    +                                            ﹈ ﹈ ﹈ 
     D.pop()            // 返回 5。栈的现状为:        4
    -                                            1  3
    -                                            ﹈ ﹈
    -D.pop()            // 返回 4。栈的现状为:    1  3
    -                                           ﹈ ﹈
    -D.pop()            // 返回 3。栈的现状为:    1
    -                                           ﹈
    +                                            1  3 
    +                                            ﹈ ﹈  
    +D.pop()            // 返回 4。栈的现状为:    1  3 
    +                                           ﹈ ﹈   
    +D.pop()            // 返回 3。栈的现状为:    1 
    +                                           ﹈   
     D.pop()            // 返回 1。现在没有栈。
     D.pop()            // 返回 -1。仍然没有栈。
     
    diff --git a/solution/1100-1199/1172.Dinner Plate Stacks/README_EN.md b/solution/1100-1199/1172.Dinner Plate Stacks/README_EN.md index a1b5f39c39692..7f1837953bb7a 100644 --- a/solution/1100-1199/1172.Dinner Plate Stacks/README_EN.md +++ b/solution/1100-1199/1172.Dinner Plate Stacks/README_EN.md @@ -42,7 +42,7 @@ tags: Output [null, null, null, null, null, null, 2, null, null, 20, 21, 5, 4, 3, 1, -1] -Explanation: +Explanation: DinnerPlates D = DinnerPlates(2); // Initialize with capacity = 2 D.push(1); D.push(2); @@ -65,14 +65,14 @@ D.popAtStack(0); // Returns 20. The stacks are now: 4 21 ﹈ ﹈ ﹈ D.popAtStack(2); // Returns 21. The stacks are now: 4 1 3 5 - ﹈ ﹈ ﹈ + ﹈ ﹈ ﹈ D.pop() // Returns 5. The stacks are now: 4 - 1 3 - ﹈ ﹈ -D.pop() // Returns 4. The stacks are now: 1 3 - ﹈ ﹈ -D.pop() // Returns 3. The stacks are now: 1 - ﹈ + 1 3 + ﹈ ﹈ +D.pop() // Returns 4. The stacks are now: 1 3 + ﹈ ﹈ +D.pop() // Returns 3. The stacks are now: 1 + ﹈ D.pop() // Returns 1. There are no stacks. D.pop() // Returns -1. There are still no stacks.
    diff --git a/solution/1300-1399/1348.Tweet Counts Per Frequency/README.md b/solution/1300-1399/1348.Tweet Counts Per Frequency/README.md index 83dbe80cffbf8..d3cf52c0d5cfe 100644 --- a/solution/1300-1399/1348.Tweet Counts Per Frequency/README.md +++ b/solution/1300-1399/1348.Tweet Counts Per Frequency/README.md @@ -68,7 +68,7 @@ tweetCounts.recordTweet("tweet3", 0); tweetCounts.recordTweet("tweet3", 60); tweetCounts.recordTweet("tweet3", 10); // "tweet3" 发布推文的时间分别是 0, 10 和 60 。 tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // 返回 [2]。统计频率是每分钟(60 秒),因此只有一个有效时间间隔 [0,60> - > 2 条推文。 -tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // 返回 [2,1]。统计频率是每分钟(60 秒),因此有两个有效时间间隔 1) [0,60> - > 2 条推文,和 2) [60,61> - > 1 条推文。 +tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // 返回 [2,1]。统计频率是每分钟(60 秒),因此有两个有效时间间隔 1) [0,60> - > 2 条推文,和 2) [60,61> - > 1 条推文。 tweetCounts.recordTweet("tweet3", 120); // "tweet3" 发布推文的时间分别是 0, 10, 60 和 120 。 tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // 返回 [4]。统计频率是每小时(3600 秒),因此只有一个有效时间间隔 [0,211> - > 4 条推文。 diff --git a/solution/1400-1499/1400.Construct K Palindrome Strings/README.md b/solution/1400-1499/1400.Construct K Palindrome Strings/README.md index b8ec712c5656a..fe8d098302ddd 100644 --- a/solution/1400-1499/1400.Construct K Palindrome Strings/README.md +++ b/solution/1400-1499/1400.Construct K Palindrome Strings/README.md @@ -21,7 +21,7 @@ tags: -

    给你一个字符串 s 和一个整数 k 。请你用 s 字符串中 所有字符 构造 k 个非空 回文串 。

    +

    给你一个字符串 s 和一个整数 k 。请你用 s 字符串中 所有字符 构造 k 个非空 回文串 。

    如果你可以用 s 中所有字符构造 k 个回文字符串,那么请你返回 True ,否则返回 False 。

    @@ -30,16 +30,16 @@ tags:

    示例 1:

    -输入:s = "annabelle", k = 2
    +输入:s = "annabelle", k = 2
     输出:true
     解释:可以用 s 中所有字符构造 2 个回文字符串。
    -一些可行的构造方案包括:"anna" + "elble","anbna" + "elle","anellena" + "b"
    +一些可行的构造方案包括:"anna" + "elble","anbna" + "elle","anellena" + "b"
     

    示例 2:

    -输入:s = "leetcode", k = 3
    +输入:s = "leetcode", k = 3
     输出:false
     解释:无法用 s 中所有字符构造 3 个回文串。
     
    @@ -47,35 +47,19 @@ tags:

    示例 3:

    -输入:s = "true", k = 4
    +输入:s = "true", k = 4
     输出:true
     解释:唯一可行的方案是让 s 中每个字符单独构成一个字符串。
     
    -

    示例 4:

    - -
    -输入:s = "yzyzyzyzyzyzyzy", k = 2
    -输出:true
    -解释:你只需要将所有的 z 放在一个字符串中,所有的 y 放在另一个字符串中。那么两个字符串都是回文串。
    -
    - -

    示例 5:

    - -
    -输入:s = "cr", k = 7
    -输出:false
    -解释:我们没有足够的字符去构造 7 个回文串。
    -
    -

     

    提示:

      -
    • 1 <= s.length <= 10^5
    • +
    • 1 <= s.length <= 105
    • s 中所有字符都是小写英文字母。
    • -
    • 1 <= k <= 10^5
    • +
    • 1 <= k <= 105
    diff --git a/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md b/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md index ff3f2f4c6b6e9..c6de7f92233ae 100644 --- a/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md +++ b/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md @@ -32,10 +32,10 @@ tags:

    示例 1:

    输入:nums = [8,2,4,7], limit = 4
    -输出:2
    +输出:2 
     解释:所有子数组如下:
     [8] 最大绝对差 |8-8| = 0 <= 4.
    -[8,2] 最大绝对差 |8-2| = 6 > 4.
    +[8,2] 最大绝对差 |8-2| = 6 > 4. 
     [8,2,4] 最大绝对差 |8-2| = 6 > 4.
     [8,2,4,7] 最大绝对差 |8-2| = 6 > 4.
     [2] 最大绝对差 |2-2| = 0 <= 4.
    @@ -43,14 +43,14 @@ tags:
     [2,4,7] 最大绝对差 |2-7| = 5 > 4.
     [4] 最大绝对差 |4-4| = 0 <= 4.
     [4,7] 最大绝对差 |4-7| = 3 <= 4.
    -[7] 最大绝对差 |7-7| = 0 <= 4.
    +[7] 最大绝对差 |7-7| = 0 <= 4. 
     因此,满足题意的最长子数组的长度为 2 。
     

    示例 2:

    输入:nums = [10,1,2,4,7,2], limit = 5
    -输出:4
    +输出:4 
     解释:满足题意的最长子数组是 [2,4,7,2],其最大绝对差 |2-7| = 5 <= 5 。
     
    diff --git a/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md b/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md index 07f167125aab5..838e7dae2e4b2 100644 --- a/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md +++ b/solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md @@ -30,10 +30,10 @@ tags:
     Input: nums = [8,2,4,7], limit = 4
    -Output: 2
    -Explanation: All subarrays are:
    +Output: 2 
    +Explanation: All subarrays are: 
     [8] with maximum absolute diff |8-8| = 0 <= 4.
    -[8,2] with maximum absolute diff |8-2| = 6 > 4.
    +[8,2] with maximum absolute diff |8-2| = 6 > 4. 
     [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
     [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
     [2] with maximum absolute diff |2-2| = 0 <= 4.
    @@ -41,7 +41,7 @@ tags:
     [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
     [4] with maximum absolute diff |4-4| = 0 <= 4.
     [4,7] with maximum absolute diff |4-7| = 3 <= 4.
    -[7] with maximum absolute diff |7-7| = 0 <= 4.
    +[7] with maximum absolute diff |7-7| = 0 <= 4. 
     Therefore, the size of the longest subarray is 2.
     
    @@ -49,7 +49,7 @@ Therefore, the size of the longest subarray is 2.
     Input: nums = [10,1,2,4,7,2], limit = 5
    -Output: 4
    +Output: 4 
     Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
     
    diff --git a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md index 8c534cf5a9efe..a78b2580463c1 100644 --- a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md +++ b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md @@ -41,8 +41,8 @@ tags:

    -输入:k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
    -输出:[1]
    +输入:k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3] 
    +输出:[1] 
     解释:
     所有服务器一开始都是空闲的。
     前 3 个请求分别由前 3 台服务器依次处理。
    diff --git a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md
    index 6c8ae0631143e..11f80e8f7c508 100644
    --- a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md	
    +++ b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md	
    @@ -38,9 +38,9 @@ tags:
     

    Example 1:

    -Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
    -Output: [1]
    -Explanation:
    +Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3] 
    +Output: [1] 
    +Explanation: 
     All of the servers start out available.
     The first 3 requests are handled by the first 3 servers in order.
     Request 3 comes in. Server 0 is busy, so it's assigned to the next available server, which is 1.
    @@ -53,7 +53,7 @@ Servers 0 and 2 handled one request each, while server 1 handled two requests. H
     
     Input: k = 3, arrival = [1,2,3,4], load = [1,2,1,2]
     Output: [0]
    -Explanation:
    +Explanation: 
     The first 3 requests are handled by first 3 servers.
     Request 3 comes in. It is handled by server 0 since the server is available.
     Server 0 handled two requests, while servers 1 and 2 handled one request each. Hence server 0 is the busiest server.
    diff --git a/solution/1800-1899/1825.Finding MK Average/README.md b/solution/1800-1899/1825.Finding MK Average/README.md
    index f7b67495227d5..fa970e64153d0 100644
    --- a/solution/1800-1899/1825.Finding MK Average/README.md	
    +++ b/solution/1800-1899/1825.Finding MK Average/README.md	
    @@ -52,7 +52,7 @@ tags:
     [null, null, null, -1, null, 3, null, null, null, 5]
     
     解释:
    -MKAverage obj = new MKAverage(3, 1);
    +MKAverage obj = new MKAverage(3, 1); 
     obj.addElement(3);        // 当前元素为 [3]
     obj.addElement(1);        // 当前元素为 [3,1]
     obj.calculateMKAverage(); // 返回 -1 ,因为 m = 3 ,但数据流中只有 2 个元素
    diff --git a/solution/1800-1899/1825.Finding MK Average/README_EN.md b/solution/1800-1899/1825.Finding MK Average/README_EN.md
    index 12b3147398684..47e210d29904b 100644
    --- a/solution/1800-1899/1825.Finding MK Average/README_EN.md	
    +++ b/solution/1800-1899/1825.Finding MK Average/README_EN.md	
    @@ -51,7 +51,7 @@ tags:
     [null, null, null, -1, null, 3, null, null, null, 5]
     
     Explanation
    -MKAverage obj = new MKAverage(3, 1);
    +MKAverage obj = new MKAverage(3, 1); 
     obj.addElement(3);        // current elements are [3]
     obj.addElement(1);        // current elements are [3,1]
     obj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist.
    diff --git a/solution/2300-2399/2349.Design a Number Container System/README_EN.md b/solution/2300-2399/2349.Design a Number Container System/README_EN.md
    index 8480f11edb6db..b55a94b3c5cfd 100644
    --- a/solution/2300-2399/2349.Design a Number Container System/README_EN.md	
    +++ b/solution/2300-2399/2349.Design a Number Container System/README_EN.md	
    @@ -54,7 +54,7 @@ nc.change(1, 10); // Your container at index 1 will be filled with number 10.
     nc.change(3, 10); // Your container at index 3 will be filled with number 10.
     nc.change(5, 10); // Your container at index 5 will be filled with number 10.
     nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
    -nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
    +nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20. 
     nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
     
    diff --git a/solution/2500-2599/2590.Design a Todo List/README.md b/solution/2500-2599/2590.Design a Todo List/README.md index c66941f981134..8a3a3cb1e1ff0 100644 --- a/solution/2500-2599/2590.Design a Todo List/README.md +++ b/solution/2500-2599/2590.Design a Todo List/README.md @@ -44,17 +44,17 @@ tags: [null, 1, 2, ["Task1", "Task2"], [], 3, ["Task3", "Task2"], null, null, ["Task3"], ["Task3", "Task1"]] 解释 -TodoList todoList = new TodoList(); -todoList.addTask(1, "Task1", 50, []); // 返回1。为ID为1的用户添加一个新任务。 -todoList.addTask(1, "Task2", 100, ["P1"]); // 返回2。为ID为1的用户添加另一个任务,并给它添加标签“P1”。 -todoList.getAllTasks(1); // 返回["Task1", "Task2"]。用户1目前有两个未完成的任务。 -todoList.getAllTasks(5); // 返回[]。用户5目前没有任务。 -todoList.addTask(1, "Task3", 30, ["P1"]); // 返回3。为ID为1的用户添加另一个任务,并给它添加标签“P1”。 -todoList.getTasksForTag(1, "P1"); // 返回["Task3", "Task2"]。返回ID为1的用户未完成的带有“P1”标签的任务。 -todoList.completeTask(5, 1); // 不做任何操作,因为任务1不属于用户5。 -todoList.completeTask(1, 2); // 将任务2标记为已完成。 -todoList.getTasksForTag(1, "P1"); // 返回["Task3"]。返回ID为1的用户未完成的带有“P1”标签的任务。 - // 注意,现在不包括 “Task2” ,因为它已经被标记为已完成。 +TodoList todoList = new TodoList(); +todoList.addTask(1, "Task1", 50, []); // 返回1。为ID为1的用户添加一个新任务。 +todoList.addTask(1, "Task2", 100, ["P1"]); // 返回2。为ID为1的用户添加另一个任务,并给它添加标签“P1”。 +todoList.getAllTasks(1); // 返回["Task1", "Task2"]。用户1目前有两个未完成的任务。 +todoList.getAllTasks(5); // 返回[]。用户5目前没有任务。 +todoList.addTask(1, "Task3", 30, ["P1"]); // 返回3。为ID为1的用户添加另一个任务,并给它添加标签“P1”。 +todoList.getTasksForTag(1, "P1"); // 返回["Task3", "Task2"]。返回ID为1的用户未完成的带有“P1”标签的任务。 +todoList.completeTask(5, 1); // 不做任何操作,因为任务1不属于用户5。 +todoList.completeTask(1, 2); // 将任务2标记为已完成。 +todoList.getTasksForTag(1, "P1"); // 返回["Task3"]。返回ID为1的用户未完成的带有“P1”标签的任务。 + // 注意,现在不包括 “Task2” ,因为它已经被标记为已完成。 todoList.getAllTasks(1); // 返回["Task3", "Task1"]。用户1现在有两个未完成的任务。
    diff --git a/solution/2600-2699/2653.Sliding Subarray Beauty/README_EN.md b/solution/2600-2699/2653.Sliding Subarray Beauty/README_EN.md index 0de9ec290eff7..0ff195c6f0e26 100644 --- a/solution/2600-2699/2653.Sliding Subarray Beauty/README_EN.md +++ b/solution/2600-2699/2653.Sliding Subarray Beauty/README_EN.md @@ -38,7 +38,7 @@ tags:
     Input: nums = [1,-1,-3,-2,3], k = 3, x = 2
     Output: [-1,-2,-2]
    -Explanation: There are 3 subarrays with size k = 3.
    +Explanation: There are 3 subarrays with size k = 3. 
     The first subarray is [1, -1, -3] and the 2nd smallest negative integer is -1. 
     The second subarray is [-1, -3, -2] and the 2nd smallest negative integer is -2. 
     The third subarray is [-3, -2, 3] and the 2nd smallest negative integer is -2.
    diff --git a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md index a017d3e80647a..45530dc18abf5 100644 --- a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md +++ b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md @@ -34,9 +34,9 @@ tags: Input: num1 = 3, num2 = -2 Output: 3 Explanation: We can make 3 equal to 0 with the following operations: -- We choose i = 2 and substract 22 + (-2) from 3, 3 - (4 + (-2)) = 1. -- We choose i = 2 and substract 22 + (-2) from 1, 1 - (4 + (-2)) = -1. -- We choose i = 0 and substract 20 + (-2) from -1, (-1) - (1 + (-2)) = 0. +- We choose i = 2 and subtract 22 + (-2) from 3, 3 - (4 + (-2)) = 1. +- We choose i = 2 and subtract 22 + (-2) from 1, 1 - (4 + (-2)) = -1. +- We choose i = 0 and subtract 20 + (-2) from -1, (-1) - (1 + (-2)) = 0. It can be proven, that 3 is the minimum number of operations that we need to perform.
    diff --git a/solution/2700-2799/2762.Continuous Subarrays/README_EN.md b/solution/2700-2799/2762.Continuous Subarrays/README_EN.md index 258846bd742cb..0b3ca5f943041 100644 --- a/solution/2700-2799/2762.Continuous Subarrays/README_EN.md +++ b/solution/2700-2799/2762.Continuous Subarrays/README_EN.md @@ -39,7 +39,7 @@ tags:
     Input: nums = [5,4,2,4]
     Output: 8
    -Explanation:
    +Explanation: 
     Continuous subarray of size 1: [5], [4], [2], [4].
     Continuous subarray of size 2: [5,4], [4,2], [2,4].
     Continuous subarray of size 3: [4,2,4].
    @@ -55,7 +55,7 @@ It can be shown that there are no more continuous subarrays.
     
     Input: nums = [1,2,3]
     Output: 6
    -Explanation:
    +Explanation: 
     Continuous subarray of size 1: [1], [2], [3].
     Continuous subarray of size 2: [1,2], [2,3].
     Continuous subarray of size 3: [1,2,3].
    diff --git a/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md b/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md
    index 0243dafa27604..ca4df4150344f 100644
    --- a/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md	
    +++ b/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md	
    @@ -43,7 +43,7 @@ tags:
     - Subarray [3, 1] with an imbalance number of 1.
     - Subarray [3, 1, 4] with an imbalance number of 1.
     - Subarray [1, 4] with an imbalance number of 1.
    -The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3.
    +The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3. 
     

    Example 2:

    @@ -55,8 +55,8 @@ The imbalance number of all other subarrays is 0. Hence, the sum of imbalance nu - Subarray [1, 3] with an imbalance number of 1. - Subarray [1, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3] with an imbalance number of 1. -- Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. -- Subarray [3, 3, 3, 5] with an imbalance number of 1. +- Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. +- Subarray [3, 3, 3, 5] with an imbalance number of 1. - Subarray [3, 3, 5] with an imbalance number of 1. - Subarray [3, 5] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8.
    diff --git a/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/README_EN.md b/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/README_EN.md index fb1c71f35d61f..a770f63c0f35c 100644 --- a/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/README_EN.md +++ b/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/README_EN.md @@ -34,8 +34,8 @@ tags:
     Input: nums = [4,3,2,4], x = 2
     Output: 0
    -Explanation: We can select nums[0] = 4 and nums[3] = 4.
    -They are at least 2 indices apart, and their absolute difference is the minimum, 0.
    +Explanation: We can select nums[0] = 4 and nums[3] = 4. 
    +They are at least 2 indices apart, and their absolute difference is the minimum, 0. 
     It can be shown that 0 is the optimal answer.
     
    diff --git a/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README.md b/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README.md index ba80832e5d3d5..f48d4d484d420 100644 --- a/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README.md +++ b/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README.md @@ -53,6 +53,7 @@ tags:
  • 从 100 分 开始。
  • 对于每一个技能,当 熟练程度 > 重要性 加 10 分。
  • 对于每一个技能,当 熟练程度 < 重要性 减 5 分。
  • +
  • 如果候选人的技能熟练程度 等于 项目的技能重要性,则分数保持不变
  • diff --git a/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README_EN.md b/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README_EN.md index d12ce33590cf9..8051f616dde66 100644 --- a/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README_EN.md +++ b/solution/3200-3299/3278.Find Candidates for Data Scientist Position II/README_EN.md @@ -53,6 +53,7 @@ Each row includes project_id, required skill, and its importance (1-5) for the p
  • Start with 100 points
  • Add 10 points for each skill where proficiency > importance
  • Subtract 5 points for each skill where proficiency < importance
  • +
  • If the candidate's skill proficiency equal to the project's skill importance, the score remains unchanged
  • diff --git a/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README.md b/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README.md index 4180a7c107bc9..19f2c5c68c963 100644 --- a/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README.md +++ b/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3416.Subsequences%20with%20a%20Unique%20Middle%20Mode%20II/README.md +tags: + - 数组 + - 哈希表 + - 数学 + - 组合数学 --- diff --git a/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README_EN.md b/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README_EN.md index 265e95e1ffc0e..95d7be3b16ca5 100644 --- a/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README_EN.md +++ b/solution/3400-3499/3416.Subsequences with a Unique Middle Mode II/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3416.Subsequences%20with%20a%20Unique%20Middle%20Mode%20II/README_EN.md +tags: + - Array + - Hash Table + - Math + - Combinatorics --- diff --git a/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README.md b/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README.md index 531abe911e23b..db79cb649ba46 100644 --- a/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README.md +++ b/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3417.Zigzag%20Grid%20Traversal%20With%20Skip/README.md +tags: + - 数组 + - 矩阵 + - 模拟 --- diff --git a/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README_EN.md b/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README_EN.md index 6d8348932c192..cb67c512fbe9c 100644 --- a/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README_EN.md +++ b/solution/3400-3499/3417.Zigzag Grid Traversal With Skip/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3417.Zigzag%20Grid%20Traversal%20With%20Skip/README_EN.md +tags: + - Array + - Matrix + - Simulation --- diff --git a/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README.md b/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README.md index dae5b2e79a82b..1cf9248c88c88 100644 --- a/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README.md +++ b/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3418.Maximum%20Amount%20of%20Money%20Robot%20Can%20Earn/README.md +tags: + - 数组 + - 动态规划 + - 矩阵 --- diff --git a/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README_EN.md b/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README_EN.md index f8967e6bf6a1c..3da9ead6009d1 100644 --- a/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README_EN.md +++ b/solution/3400-3499/3418.Maximum Amount of Money Robot Can Earn/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3418.Maximum%20Amount%20of%20Money%20Robot%20Can%20Earn/README_EN.md +tags: + - Array + - Dynamic Programming + - Matrix --- diff --git a/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README.md b/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README.md index 0bb19adce250e..368c41316ab7b 100644 --- a/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README.md +++ b/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3419.Minimize%20the%20Maximum%20Edge%20Weight%20of%20Graph/README.md +tags: + - 深度优先搜索 + - 广度优先搜索 + - 图 + - 二分查找 + - 最短路 --- diff --git a/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README_EN.md b/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README_EN.md index 491856f4f48b3..3dce5b51234ae 100644 --- a/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README_EN.md +++ b/solution/3400-3499/3419.Minimize the Maximum Edge Weight of Graph/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3419.Minimize%20the%20Maximum%20Edge%20Weight%20of%20Graph/README_EN.md +tags: + - Depth-First Search + - Breadth-First Search + - Graph + - Binary Search + - Shortest Path --- diff --git a/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README.md b/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README.md index f92e49178501e..965d09261155c 100644 --- a/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README.md +++ b/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README.md @@ -2,6 +2,14 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3420.Count%20Non-Decreasing%20Subarrays%20After%20K%20Operations/README.md +tags: + - 栈 + - 线段树 + - 队列 + - 数组 + - 双指针 + - 单调队列 + - 单调栈 --- diff --git a/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README_EN.md b/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README_EN.md index 5af7c0c0a4918..57af007720f84 100644 --- a/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README_EN.md +++ b/solution/3400-3499/3420.Count Non-Decreasing Subarrays After K Operations/README_EN.md @@ -2,6 +2,14 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3420.Count%20Non-Decreasing%20Subarrays%20After%20K%20Operations/README_EN.md +tags: + - Stack + - Segment Tree + - Queue + - Array + - Two Pointers + - Monotonic Queue + - Monotonic Stack --- diff --git a/solution/3400-3499/3421.Find Students Who Improved/README.md b/solution/3400-3499/3421.Find Students Who Improved/README.md index 49310b9f66563..b47eecd7f549d 100644 --- a/solution/3400-3499/3421.Find Students Who Improved/README.md +++ b/solution/3400-3499/3421.Find Students Who Improved/README.md @@ -86,7 +86,7 @@ tags:
  • 学生 101 的物理:没有进步(从 65 分退步到 60分)
  • 学生 102 的数学:从 80 进步到 85 分。
  • 学生 103 的数学:只有一次考试,不符合资格。
  • -
  • 学生 104 in Physics:从 75 分进步到 85 分。
  • +
  • 学生 104 的物理:从 75 分进步到 85 分。
  • 结果表以 student_id,subject 升序排序。

    diff --git a/solution/3400-3499/3422.Minimum Operations to Make Subarray Elements Equal/README.md b/solution/3400-3499/3422.Minimum Operations to Make Subarray Elements Equal/README.md index 1be8c45c55821..ad4685f9375f9 100644 --- a/solution/3400-3499/3422.Minimum Operations to Make Subarray Elements Equal/README.md +++ b/solution/3400-3499/3422.Minimum Operations to Make Subarray Elements Equal/README.md @@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3422.Mi -# [3422. Minimum Operations to Make Subarray Elements Equal 🔒](https://leetcode.cn/problems/minimum-operations-to-make-subarray-elements-equal) +# [3422. 将子数组元素变为相等所需的最小操作数 🔒](https://leetcode.cn/problems/minimum-operations-to-make-subarray-elements-equal) [English Version](/solution/3400-3499/3422.Minimum%20Operations%20to%20Make%20Subarray%20Elements%20Equal/README_EN.md) @@ -14,49 +14,51 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3422.Mi -

    You are given an integer array nums and an integer k. You can perform the following operation any number of times:

    +

    给定一个整数数组 nums 和一个整数 k。你可以进行任意次以下操作:

      -
    • Increase or decrease any element of nums by 1.
    • +
    • 给 nums 的任何元素增加或减少 1。
    -

    Return the minimum number of operations required to ensure that at least one subarray of size k in nums has all elements equal.

    +

    返回确保 至少 有一个大小为 k 的 nums 中的 子数组 的所有元素都相等的所需的 最小 操作数。

     

    -

    Example 1:

    + +

    示例 1:

    -

    Input: nums = [4,-3,2,1,-4,6], k = 3

    +

    输入:nums = [4,-3,2,1,-4,6], k = 3

    -

    Output: 5

    +

    输出:5

    -

    Explanation:

    +

    解释:

      -
    • Use 4 operations to add 4 to nums[1]. The resulting array is [4, 1, 2, 1, -4, 6].
    • -
    • Use 1 operation to subtract 1 from nums[2]. The resulting array is [4, 1, 1, 1, -4, 6].
    • -
    • The array now contains a subarray [1, 1, 1] of size k = 3 with all elements equal. Hence, the answer is 5.
    • +
    • 使用 4 次操作来给 nums[1] 增加 4。结果数组为 [4, 1, 2, 1, -4, 6]
    • +
    • 使用 1 次操作来给 nums[2] 减少 1。结果数组为 [4, 1, 1, 1, -4, 6]
    • +
    • 现在数组包含一个大小为 k = 3 的子数组 [1, 1, 1],所有元素都想等。因此,答案为 5。
    -

    Example 2:

    +

    示例 2:

    -

    Input: nums = [-2,-2,3,1,4], k = 2

    +

    输入:nums = [-2,-2,3,1,4], k = 2

    -

    Output: 0

    +

    输出:0

    -

    Explanation:

    +

    解释:

    • -

      The subarray [-2, -2] of size k = 2 already contains all equal elements, so no operations are needed. Hence, the answer is 0.

      +

      大小为 k = 2 的子数组 [-2, -2] 已经包含了所有相等的元素,所以不需要操作。因此答案为 0。

     

    -

    Constraints:

    + +

    提示:

    • 2 <= nums.length <= 105
    • diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index dcd10873eb2c9..6e1e4e6035dcc 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -307,7 +307,7 @@ | 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README.md) | `数据库` | 困难 | 🔒 | | 3401 | [Find Circular Gift Exchange Chains](/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README.md) | `数据库` | 困难 | 🔒 | | 3415 | [查找具有三个连续数字的产品](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README.md) | `数据库` | 简单 | 🔒 | -| 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | | 中等 | | +| 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | `数据库` | 中等 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 1293d46f2f4e6..3043b75616d6c 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -305,7 +305,7 @@ Press Control + F(or Command + F on | 3390 | [Longest Team Pass Streak](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README_EN.md) | `Database` | Hard | 🔒 | | 3401 | [Find Circular Gift Exchange Chains](/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README_EN.md) | `Database` | Hard | 🔒 | | 3415 | [Find Products with Three Consecutive Digits](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README_EN.md) | `Database` | Easy | 🔒 | -| 3421 | [Find Students Who Improved](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README_EN.md) | | Medium | | +| 3421 | [Find Students Who Improved](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README_EN.md) | `Database` | Medium | | ## Copyright diff --git a/solution/README.md b/solution/README.md index 9e5e9333ca9a2..48c96991abc65 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3426,13 +3426,13 @@ | 3413 | [收集连续 K 个袋子可以获得的最多硬币数量](/solution/3400-3499/3413.Maximum%20Coins%20From%20K%20Consecutive%20Bags/README.md) | `贪心`,`数组`,`二分查找`,`前缀和`,`排序`,`滑动窗口` | 中等 | 第 431 场周赛 | | 3414 | [不重叠区间的最大得分](/solution/3400-3499/3414.Maximum%20Score%20of%20Non-overlapping%20Intervals/README.md) | `数组`,`二分查找`,`动态规划`,`排序` | 困难 | 第 431 场周赛 | | 3415 | [查找具有三个连续数字的产品](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README.md) | `数据库` | 简单 | 🔒 | -| 3416 | [唯一中间众数子序列 II](/solution/3400-3499/3416.Subsequences%20with%20a%20Unique%20Middle%20Mode%20II/README.md) | | 困难 | 🔒 | -| 3417 | [跳过交替单元格的之字形遍历](/solution/3400-3499/3417.Zigzag%20Grid%20Traversal%20With%20Skip/README.md) | | 简单 | 第 432 场周赛 | -| 3418 | [机器人可以获得的最大金币数](/solution/3400-3499/3418.Maximum%20Amount%20of%20Money%20Robot%20Can%20Earn/README.md) | | 中等 | 第 432 场周赛 | -| 3419 | [图的最大边权的最小值](/solution/3400-3499/3419.Minimize%20the%20Maximum%20Edge%20Weight%20of%20Graph/README.md) | | 中等 | 第 432 场周赛 | -| 3420 | [统计 K 次操作以内得到非递减子数组的数目](/solution/3400-3499/3420.Count%20Non-Decreasing%20Subarrays%20After%20K%20Operations/README.md) | | 困难 | 第 432 场周赛 | -| 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | | 中等 | | -| 3422 | [Minimum Operations to Make Subarray Elements Equal](/solution/3400-3499/3422.Minimum%20Operations%20to%20Make%20Subarray%20Elements%20Equal/README.md) | | 中等 | 🔒 | +| 3416 | [唯一中间众数子序列 II](/solution/3400-3499/3416.Subsequences%20with%20a%20Unique%20Middle%20Mode%20II/README.md) | `数组`,`哈希表`,`数学`,`组合数学` | 困难 | 🔒 | +| 3417 | [跳过交替单元格的之字形遍历](/solution/3400-3499/3417.Zigzag%20Grid%20Traversal%20With%20Skip/README.md) | `数组`,`矩阵`,`模拟` | 简单 | 第 432 场周赛 | +| 3418 | [机器人可以获得的最大金币数](/solution/3400-3499/3418.Maximum%20Amount%20of%20Money%20Robot%20Can%20Earn/README.md) | `数组`,`动态规划`,`矩阵` | 中等 | 第 432 场周赛 | +| 3419 | [图的最大边权的最小值](/solution/3400-3499/3419.Minimize%20the%20Maximum%20Edge%20Weight%20of%20Graph/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`二分查找`,`最短路` | 中等 | 第 432 场周赛 | +| 3420 | [统计 K 次操作以内得到非递减子数组的数目](/solution/3400-3499/3420.Count%20Non-Decreasing%20Subarrays%20After%20K%20Operations/README.md) | `栈`,`线段树`,`队列`,`数组`,`双指针`,`单调队列`,`单调栈` | 困难 | 第 432 场周赛 | +| 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | `数据库` | 中等 | | +| 3422 | [将子数组元素变为相等所需的最小操作数](/solution/3400-3499/3422.Minimum%20Operations%20to%20Make%20Subarray%20Elements%20Equal/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 8c913ea30f7bc..08b7fedd873c0 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3424,12 +3424,12 @@ Press Control + F(or Command + F on | 3413 | [Maximum Coins From K Consecutive Bags](/solution/3400-3499/3413.Maximum%20Coins%20From%20K%20Consecutive%20Bags/README_EN.md) | `Greedy`,`Array`,`Binary Search`,`Prefix Sum`,`Sorting`,`Sliding Window` | Medium | Weekly Contest 431 | | 3414 | [Maximum Score of Non-overlapping Intervals](/solution/3400-3499/3414.Maximum%20Score%20of%20Non-overlapping%20Intervals/README_EN.md) | `Array`,`Binary Search`,`Dynamic Programming`,`Sorting` | Hard | Weekly Contest 431 | | 3415 | [Find Products with Three Consecutive Digits](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README_EN.md) | `Database` | Easy | 🔒 | -| 3416 | [Subsequences with a Unique Middle Mode II](/solution/3400-3499/3416.Subsequences%20with%20a%20Unique%20Middle%20Mode%20II/README_EN.md) | | Hard | 🔒 | -| 3417 | [Zigzag Grid Traversal With Skip](/solution/3400-3499/3417.Zigzag%20Grid%20Traversal%20With%20Skip/README_EN.md) | | Easy | Weekly Contest 432 | -| 3418 | [Maximum Amount of Money Robot Can Earn](/solution/3400-3499/3418.Maximum%20Amount%20of%20Money%20Robot%20Can%20Earn/README_EN.md) | | Medium | Weekly Contest 432 | -| 3419 | [Minimize the Maximum Edge Weight of Graph](/solution/3400-3499/3419.Minimize%20the%20Maximum%20Edge%20Weight%20of%20Graph/README_EN.md) | | Medium | Weekly Contest 432 | -| 3420 | [Count Non-Decreasing Subarrays After K Operations](/solution/3400-3499/3420.Count%20Non-Decreasing%20Subarrays%20After%20K%20Operations/README_EN.md) | | Hard | Weekly Contest 432 | -| 3421 | [Find Students Who Improved](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README_EN.md) | | Medium | | +| 3416 | [Subsequences with a Unique Middle Mode II](/solution/3400-3499/3416.Subsequences%20with%20a%20Unique%20Middle%20Mode%20II/README_EN.md) | `Array`,`Hash Table`,`Math`,`Combinatorics` | Hard | 🔒 | +| 3417 | [Zigzag Grid Traversal With Skip](/solution/3400-3499/3417.Zigzag%20Grid%20Traversal%20With%20Skip/README_EN.md) | `Array`,`Matrix`,`Simulation` | Easy | Weekly Contest 432 | +| 3418 | [Maximum Amount of Money Robot Can Earn](/solution/3400-3499/3418.Maximum%20Amount%20of%20Money%20Robot%20Can%20Earn/README_EN.md) | `Array`,`Dynamic Programming`,`Matrix` | Medium | Weekly Contest 432 | +| 3419 | [Minimize the Maximum Edge Weight of Graph](/solution/3400-3499/3419.Minimize%20the%20Maximum%20Edge%20Weight%20of%20Graph/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Binary Search`,`Shortest Path` | Medium | Weekly Contest 432 | +| 3420 | [Count Non-Decreasing Subarrays After K Operations](/solution/3400-3499/3420.Count%20Non-Decreasing%20Subarrays%20After%20K%20Operations/README_EN.md) | `Stack`,`Segment Tree`,`Queue`,`Array`,`Two Pointers`,`Monotonic Queue`,`Monotonic Stack` | Hard | Weekly Contest 432 | +| 3421 | [Find Students Who Improved](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README_EN.md) | `Database` | Medium | | | 3422 | [Minimum Operations to Make Subarray Elements Equal](/solution/3400-3499/3422.Minimum%20Operations%20to%20Make%20Subarray%20Elements%20Equal/README_EN.md) | | Medium | 🔒 | ## Copyright