diff --git a/images/starcharts.svg b/images/starcharts.svg
index 4892d2d407287..8db01cbf4daa5 100644
--- a/images/starcharts.svg
+++ b/images/starcharts.svg
@@ -1,4 +1,4 @@
-
+
\ No newline at end of file
+L 945 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 946 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 947 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 16
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 948 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 949 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15
+L 950 15" style="stroke-width:2;stroke:rgba(129,199,239,1.0);fill:none"/>
\ No newline at end of file
diff --git a/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md b/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md
index cec5a5d000ce6..f4936db52f480 100644
--- a/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md
+++ b/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md
@@ -310,6 +310,75 @@ function numberOfPowerfulInt(start: number, finish: number, limit: number, s: st
}
```
+#### Rust
+
+```rust
+impl Solution {
+ pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
+ fn count(x: i64, limit: i32, s: &str) -> i64 {
+ let t = x.to_string();
+ if t.len() < s.len() {
+ return 0;
+ }
+
+ let t_bytes: Vec = t.bytes().collect();
+ let mut f = [-1_i64; 20];
+
+ fn dfs(
+ pos: usize,
+ lim: bool,
+ t: &[u8],
+ s: &str,
+ limit: i32,
+ f: &mut [i64; 20],
+ ) -> i64 {
+ if t.len() < s.len() {
+ return 0;
+ }
+
+ if !lim && f[pos] != -1 {
+ return f[pos];
+ }
+
+ if t.len() - pos == s.len() {
+ if lim {
+ let suffix = &t[pos..];
+ let suffix_str = String::from_utf8_lossy(suffix);
+ return if suffix_str.as_ref() >= s { 1 } else { 0 };
+ } else {
+ return 1;
+ }
+ }
+
+ let mut ans = 0;
+ let up = if lim {
+ (t[pos] - b'0').min(limit as u8)
+ } else {
+ limit as u8
+ };
+
+ for i in 0..=up {
+ let next_lim = lim && i == t[pos] - b'0';
+ ans += dfs(pos + 1, next_lim, t, s, limit, f);
+ }
+
+ if !lim {
+ f[pos] = ans;
+ }
+
+ ans
+ }
+
+ dfs(0, true, &t_bytes, s, limit, &mut f)
+ }
+
+ let a = count(start - 1, limit, &s);
+ let b = count(finish, limit, &s);
+ b - a
+ }
+}
+```
+
#### C#
```cs
diff --git a/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md b/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md
index 5ffa6ef4c116e..f2cf88994f79b 100644
--- a/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md
+++ b/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md
@@ -308,6 +308,75 @@ function numberOfPowerfulInt(start: number, finish: number, limit: number, s: st
}
```
+#### Rust
+
+```rust
+impl Solution {
+ pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
+ fn count(x: i64, limit: i32, s: &str) -> i64 {
+ let t = x.to_string();
+ if t.len() < s.len() {
+ return 0;
+ }
+
+ let t_bytes: Vec = t.bytes().collect();
+ let mut f = [-1_i64; 20];
+
+ fn dfs(
+ pos: usize,
+ lim: bool,
+ t: &[u8],
+ s: &str,
+ limit: i32,
+ f: &mut [i64; 20],
+ ) -> i64 {
+ if t.len() < s.len() {
+ return 0;
+ }
+
+ if !lim && f[pos] != -1 {
+ return f[pos];
+ }
+
+ if t.len() - pos == s.len() {
+ if lim {
+ let suffix = &t[pos..];
+ let suffix_str = String::from_utf8_lossy(suffix);
+ return if suffix_str.as_ref() >= s { 1 } else { 0 };
+ } else {
+ return 1;
+ }
+ }
+
+ let mut ans = 0;
+ let up = if lim {
+ (t[pos] - b'0').min(limit as u8)
+ } else {
+ limit as u8
+ };
+
+ for i in 0..=up {
+ let next_lim = lim && i == t[pos] - b'0';
+ ans += dfs(pos + 1, next_lim, t, s, limit, f);
+ }
+
+ if !lim {
+ f[pos] = ans;
+ }
+
+ ans
+ }
+
+ dfs(0, true, &t_bytes, s, limit, &mut f)
+ }
+
+ let a = count(start - 1, limit, &s);
+ let b = count(finish, limit, &s);
+ b - a
+ }
+}
+```
+
#### C#
```cs
diff --git a/solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs b/solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs
new file mode 100644
index 0000000000000..2ce88e7dd89ca
--- /dev/null
+++ b/solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs
@@ -0,0 +1,57 @@
+impl Solution {
+ pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
+ fn count(x: i64, limit: i32, s: &str) -> i64 {
+ let t = x.to_string();
+ if t.len() < s.len() {
+ return 0;
+ }
+
+ let t_bytes: Vec = t.bytes().collect();
+ let mut f = [-1_i64; 20];
+
+ fn dfs(pos: usize, lim: bool, t: &[u8], s: &str, limit: i32, f: &mut [i64; 20]) -> i64 {
+ if t.len() < s.len() {
+ return 0;
+ }
+
+ if !lim && f[pos] != -1 {
+ return f[pos];
+ }
+
+ if t.len() - pos == s.len() {
+ if lim {
+ let suffix = &t[pos..];
+ let suffix_str = String::from_utf8_lossy(suffix);
+ return if suffix_str.as_ref() >= s { 1 } else { 0 };
+ } else {
+ return 1;
+ }
+ }
+
+ let mut ans = 0;
+ let up = if lim {
+ (t[pos] - b'0').min(limit as u8)
+ } else {
+ limit as u8
+ };
+
+ for i in 0..=up {
+ let next_lim = lim && i == t[pos] - b'0';
+ ans += dfs(pos + 1, next_lim, t, s, limit, f);
+ }
+
+ if !lim {
+ f[pos] = ans;
+ }
+
+ ans
+ }
+
+ dfs(0, true, &t_bytes, s, limit, &mut f)
+ }
+
+ let a = count(start - 1, limit, &s);
+ let b = count(finish, limit, &s);
+ b - a
+ }
+}
diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md
index f1e73283c5c07..6450fffdd0f8e 100644
--- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md
+++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md
@@ -176,16 +176,25 @@ func minOperations(nums []int, k int) int {
```ts
function minOperations(nums: number[], k: number): number {
- const s = new Set();
- let mi = Infinity;
+ const s = new Set([k]);
for (const x of nums) {
- if (x < k) {
- return -1;
- }
+ if (x < k) return -1;
+ s.add(x);
+ }
+ return s.size - 1;
+}
+```
+
+#### JavaScript
+
+```js
+function minOperations(nums, k) {
+ const s = new Set([k]);
+ for (const x of nums) {
+ if (x < k) return -1;
s.add(x);
- mi = Math.min(mi, x);
}
- return s.size - (mi === k ? 1 : 0);
+ return s.size - 1;
}
```
diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md
index 2dd2f6dd1d718..3c00a12599575 100644
--- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md
+++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md
@@ -174,16 +174,25 @@ func minOperations(nums []int, k int) int {
```ts
function minOperations(nums: number[], k: number): number {
- const s = new Set();
- let mi = Infinity;
+ const s = new Set([k]);
for (const x of nums) {
- if (x < k) {
- return -1;
- }
+ if (x < k) return -1;
+ s.add(x);
+ }
+ return s.size - 1;
+}
+```
+
+#### JavaScript
+
+```js
+function minOperations(nums, k) {
+ const s = new Set([k]);
+ for (const x of nums) {
+ if (x < k) return -1;
s.add(x);
- mi = Math.min(mi, x);
}
- return s.size - (mi === k ? 1 : 0);
+ return s.size - 1;
}
```
diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js
new file mode 100644
index 0000000000000..e2ebea0fa1fe8
--- /dev/null
+++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js
@@ -0,0 +1,8 @@
+function minOperations(nums, k) {
+ const s = new Set([k]);
+ for (const x of nums) {
+ if (x < k) return -1;
+ s.add(x);
+ }
+ return s.size - 1;
+}
diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts
index 652b303832721..4f22815234416 100644
--- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts
+++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts
@@ -1,12 +1,8 @@
function minOperations(nums: number[], k: number): number {
- const s = new Set();
- let mi = Infinity;
+ const s = new Set([k]);
for (const x of nums) {
- if (x < k) {
- return -1;
- }
+ if (x < k) return -1;
s.add(x);
- mi = Math.min(mi, x);
}
- return s.size - (mi === k ? 1 : 0);
+ return s.size - 1;
}
diff --git a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md
index e7fc56d1e6e20..1cc9dda7c9806 100644
--- a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md
+++ b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md
@@ -114,13 +114,248 @@ tags:
#### Java
```java
-
+class Solution {
+ static class TrieNode {
+ int count = 0;
+ int depth = 0;
+ int[] children = new int[26];
+
+ TrieNode() {
+ for (int i = 0; i < 26; ++i) children[i] = -1;
+ }
+ }
+
+ static class SegmentTree {
+ int n;
+ int[] tree;
+ int[] globalCount;
+
+ SegmentTree(int n, int[] globalCount) {
+ this.n = n;
+ this.globalCount = globalCount;
+ this.tree = new int[4 * (n + 1)];
+ for (int i = 0; i < tree.length; i++) tree[i] = -1;
+ build(1, 1, n);
+ }
+
+ void build(int idx, int l, int r) {
+ if (l == r) {
+ tree[idx] = globalCount[l] > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ build(idx * 2, l, mid);
+ build(idx * 2 + 1, mid + 1, r);
+ tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+
+ void update(int idx, int l, int r, int pos, int newVal) {
+ if (l == r) {
+ tree[idx] = newVal > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ if (pos <= mid) {
+ update(idx * 2, l, mid, pos, newVal);
+ } else {
+ update(idx * 2 + 1, mid + 1, r, pos, newVal);
+ }
+ tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+
+ int query() {
+ return tree[1];
+ }
+ }
+
+ public int[] longestCommonPrefix(String[] words, int k) {
+ int n = words.length;
+ int[] ans = new int[n];
+ if (n - 1 < k) return ans;
+
+ ArrayList trie = new ArrayList<>();
+ trie.add(new TrieNode());
+
+ for (String word : words) {
+ int cur = 0;
+ for (char c : word.toCharArray()) {
+ int idx = c - 'a';
+ if (trie.get(cur).children[idx] == -1) {
+ trie.get(cur).children[idx] = trie.size();
+ TrieNode node = new TrieNode();
+ node.depth = trie.get(cur).depth + 1;
+ trie.add(node);
+ }
+ cur = trie.get(cur).children[idx];
+ trie.get(cur).count++;
+ }
+ }
+
+ int maxDepth = 0;
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie.get(i).count >= k) {
+ maxDepth = Math.max(maxDepth, trie.get(i).depth);
+ }
+ }
+
+ int[] globalCount = new int[maxDepth + 1];
+ for (int i = 1; i < trie.size(); ++i) {
+ TrieNode node = trie.get(i);
+ if (node.count >= k && node.depth <= maxDepth) {
+ globalCount[node.depth]++;
+ }
+ }
+
+ List> fragileList = new ArrayList<>();
+ for (int i = 0; i < n; ++i) {
+ fragileList.add(new ArrayList<>());
+ }
+
+ for (int i = 0; i < n; ++i) {
+ int cur = 0;
+ for (char c : words[i].toCharArray()) {
+ int idx = c - 'a';
+ cur = trie.get(cur).children[idx];
+ if (trie.get(cur).count == k) {
+ fragileList.get(i).add(trie.get(cur).depth);
+ }
+ }
+ }
+
+ int segSize = maxDepth;
+ if (segSize >= 1) {
+ SegmentTree segTree = new SegmentTree(segSize, globalCount);
+ for (int i = 0; i < n; ++i) {
+ if (n - 1 < k) {
+ ans[i] = 0;
+ } else {
+ for (int d : fragileList.get(i)) {
+ segTree.update(1, 1, segSize, d, globalCount[d] - 1);
+ }
+ int res = segTree.query();
+ ans[i] = res == -1 ? 0 : res;
+ for (int d : fragileList.get(i)) {
+ segTree.update(1, 1, segSize, d, globalCount[d]);
+ }
+ }
+ }
+ }
+
+ return ans;
+ }
+}
```
#### C++
```cpp
-
+class Solution {
+public:
+ struct TrieNode {
+ int count = 0;
+ int depth = 0;
+ int children[26] = {0};
+ };
+
+ class SegmentTree {
+ public:
+ int n;
+ vector tree;
+ vector& globalCount;
+ SegmentTree(int n, vector& globalCount)
+ : n(n)
+ , globalCount(globalCount) {
+ tree.assign(4 * (n + 1), -1);
+ build(1, 1, n);
+ }
+ void build(int idx, int l, int r) {
+ if (l == r) {
+ tree[idx] = globalCount[l] > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ build(idx * 2, l, mid);
+ build(idx * 2 + 1, mid + 1, r);
+ tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+ void update(int idx, int l, int r, int pos, int newVal) {
+ if (l == r) {
+ tree[idx] = newVal > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ if (pos <= mid)
+ update(idx * 2, l, mid, pos, newVal);
+ else
+ update(idx * 2 + 1, mid + 1, r, pos, newVal);
+ tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+ int query() {
+ return tree[1];
+ }
+ };
+
+ vector longestCommonPrefix(vector& words, int k) {
+ int n = words.size();
+ vector ans(n, 0);
+ if (n - 1 < k) return ans;
+ vector trie(1);
+ for (const string& word : words) {
+ int cur = 0;
+ for (char c : word) {
+ int idx = c - 'a';
+ if (trie[cur].children[idx] == 0) {
+ trie[cur].children[idx] = trie.size();
+ trie.push_back({0, trie[cur].depth + 1});
+ }
+ cur = trie[cur].children[idx];
+ trie[cur].count++;
+ }
+ }
+ int maxDepth = 0;
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie[i].count >= k) {
+ maxDepth = max(maxDepth, trie[i].depth);
+ }
+ }
+ vector globalCount(maxDepth + 1, 0);
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie[i].count >= k && trie[i].depth <= maxDepth) {
+ globalCount[trie[i].depth]++;
+ }
+ }
+ vector> fragileList(n);
+ for (int i = 0; i < n; ++i) {
+ int cur = 0;
+ for (char c : words[i]) {
+ int idx = c - 'a';
+ cur = trie[cur].children[idx];
+ if (trie[cur].count == k) {
+ fragileList[i].push_back(trie[cur].depth);
+ }
+ }
+ }
+ int segSize = maxDepth;
+ if (segSize >= 1) {
+ SegmentTree segTree(segSize, globalCount);
+ for (int i = 0; i < n; ++i) {
+ if (n - 1 < k) {
+ ans[i] = 0;
+ } else {
+ for (int d : fragileList[i]) {
+ segTree.update(1, 1, segSize, d, globalCount[d] - 1);
+ }
+ int res = segTree.query();
+ ans[i] = res == -1 ? 0 : res;
+ for (int d : fragileList[i]) {
+ segTree.update(1, 1, segSize, d, globalCount[d]);
+ }
+ }
+ }
+ }
+ return ans;
+ }
+};
```
#### Go
diff --git a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md
index 1669f53620258..e2f26d7aba3ae 100644
--- a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md
+++ b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README_EN.md
@@ -108,13 +108,248 @@ tags:
#### Java
```java
-
+class Solution {
+ static class TrieNode {
+ int count = 0;
+ int depth = 0;
+ int[] children = new int[26];
+
+ TrieNode() {
+ for (int i = 0; i < 26; ++i) children[i] = -1;
+ }
+ }
+
+ static class SegmentTree {
+ int n;
+ int[] tree;
+ int[] globalCount;
+
+ SegmentTree(int n, int[] globalCount) {
+ this.n = n;
+ this.globalCount = globalCount;
+ this.tree = new int[4 * (n + 1)];
+ for (int i = 0; i < tree.length; i++) tree[i] = -1;
+ build(1, 1, n);
+ }
+
+ void build(int idx, int l, int r) {
+ if (l == r) {
+ tree[idx] = globalCount[l] > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ build(idx * 2, l, mid);
+ build(idx * 2 + 1, mid + 1, r);
+ tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+
+ void update(int idx, int l, int r, int pos, int newVal) {
+ if (l == r) {
+ tree[idx] = newVal > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ if (pos <= mid) {
+ update(idx * 2, l, mid, pos, newVal);
+ } else {
+ update(idx * 2 + 1, mid + 1, r, pos, newVal);
+ }
+ tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+
+ int query() {
+ return tree[1];
+ }
+ }
+
+ public int[] longestCommonPrefix(String[] words, int k) {
+ int n = words.length;
+ int[] ans = new int[n];
+ if (n - 1 < k) return ans;
+
+ ArrayList trie = new ArrayList<>();
+ trie.add(new TrieNode());
+
+ for (String word : words) {
+ int cur = 0;
+ for (char c : word.toCharArray()) {
+ int idx = c - 'a';
+ if (trie.get(cur).children[idx] == -1) {
+ trie.get(cur).children[idx] = trie.size();
+ TrieNode node = new TrieNode();
+ node.depth = trie.get(cur).depth + 1;
+ trie.add(node);
+ }
+ cur = trie.get(cur).children[idx];
+ trie.get(cur).count++;
+ }
+ }
+
+ int maxDepth = 0;
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie.get(i).count >= k) {
+ maxDepth = Math.max(maxDepth, trie.get(i).depth);
+ }
+ }
+
+ int[] globalCount = new int[maxDepth + 1];
+ for (int i = 1; i < trie.size(); ++i) {
+ TrieNode node = trie.get(i);
+ if (node.count >= k && node.depth <= maxDepth) {
+ globalCount[node.depth]++;
+ }
+ }
+
+ List> fragileList = new ArrayList<>();
+ for (int i = 0; i < n; ++i) {
+ fragileList.add(new ArrayList<>());
+ }
+
+ for (int i = 0; i < n; ++i) {
+ int cur = 0;
+ for (char c : words[i].toCharArray()) {
+ int idx = c - 'a';
+ cur = trie.get(cur).children[idx];
+ if (trie.get(cur).count == k) {
+ fragileList.get(i).add(trie.get(cur).depth);
+ }
+ }
+ }
+
+ int segSize = maxDepth;
+ if (segSize >= 1) {
+ SegmentTree segTree = new SegmentTree(segSize, globalCount);
+ for (int i = 0; i < n; ++i) {
+ if (n - 1 < k) {
+ ans[i] = 0;
+ } else {
+ for (int d : fragileList.get(i)) {
+ segTree.update(1, 1, segSize, d, globalCount[d] - 1);
+ }
+ int res = segTree.query();
+ ans[i] = res == -1 ? 0 : res;
+ for (int d : fragileList.get(i)) {
+ segTree.update(1, 1, segSize, d, globalCount[d]);
+ }
+ }
+ }
+ }
+
+ return ans;
+ }
+}
```
#### C++
```cpp
-
+class Solution {
+public:
+ struct TrieNode {
+ int count = 0;
+ int depth = 0;
+ int children[26] = {0};
+ };
+
+ class SegmentTree {
+ public:
+ int n;
+ vector tree;
+ vector& globalCount;
+ SegmentTree(int n, vector& globalCount)
+ : n(n)
+ , globalCount(globalCount) {
+ tree.assign(4 * (n + 1), -1);
+ build(1, 1, n);
+ }
+ void build(int idx, int l, int r) {
+ if (l == r) {
+ tree[idx] = globalCount[l] > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ build(idx * 2, l, mid);
+ build(idx * 2 + 1, mid + 1, r);
+ tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+ void update(int idx, int l, int r, int pos, int newVal) {
+ if (l == r) {
+ tree[idx] = newVal > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ if (pos <= mid)
+ update(idx * 2, l, mid, pos, newVal);
+ else
+ update(idx * 2 + 1, mid + 1, r, pos, newVal);
+ tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+ int query() {
+ return tree[1];
+ }
+ };
+
+ vector longestCommonPrefix(vector& words, int k) {
+ int n = words.size();
+ vector ans(n, 0);
+ if (n - 1 < k) return ans;
+ vector trie(1);
+ for (const string& word : words) {
+ int cur = 0;
+ for (char c : word) {
+ int idx = c - 'a';
+ if (trie[cur].children[idx] == 0) {
+ trie[cur].children[idx] = trie.size();
+ trie.push_back({0, trie[cur].depth + 1});
+ }
+ cur = trie[cur].children[idx];
+ trie[cur].count++;
+ }
+ }
+ int maxDepth = 0;
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie[i].count >= k) {
+ maxDepth = max(maxDepth, trie[i].depth);
+ }
+ }
+ vector globalCount(maxDepth + 1, 0);
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie[i].count >= k && trie[i].depth <= maxDepth) {
+ globalCount[trie[i].depth]++;
+ }
+ }
+ vector> fragileList(n);
+ for (int i = 0; i < n; ++i) {
+ int cur = 0;
+ for (char c : words[i]) {
+ int idx = c - 'a';
+ cur = trie[cur].children[idx];
+ if (trie[cur].count == k) {
+ fragileList[i].push_back(trie[cur].depth);
+ }
+ }
+ }
+ int segSize = maxDepth;
+ if (segSize >= 1) {
+ SegmentTree segTree(segSize, globalCount);
+ for (int i = 0; i < n; ++i) {
+ if (n - 1 < k) {
+ ans[i] = 0;
+ } else {
+ for (int d : fragileList[i]) {
+ segTree.update(1, 1, segSize, d, globalCount[d] - 1);
+ }
+ int res = segTree.query();
+ ans[i] = res == -1 ? 0 : res;
+ for (int d : fragileList[i]) {
+ segTree.update(1, 1, segSize, d, globalCount[d]);
+ }
+ }
+ }
+ }
+ return ans;
+ }
+};
```
#### Go
diff --git a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/Solution.cpp b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/Solution.cpp
new file mode 100644
index 0000000000000..fa4ada62e21a0
--- /dev/null
+++ b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/Solution.cpp
@@ -0,0 +1,107 @@
+class Solution {
+public:
+ struct TrieNode {
+ int count = 0;
+ int depth = 0;
+ int children[26] = {0};
+ };
+
+ class SegmentTree {
+ public:
+ int n;
+ vector tree;
+ vector& globalCount;
+ SegmentTree(int n, vector& globalCount)
+ : n(n)
+ , globalCount(globalCount) {
+ tree.assign(4 * (n + 1), -1);
+ build(1, 1, n);
+ }
+ void build(int idx, int l, int r) {
+ if (l == r) {
+ tree[idx] = globalCount[l] > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ build(idx * 2, l, mid);
+ build(idx * 2 + 1, mid + 1, r);
+ tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+ void update(int idx, int l, int r, int pos, int newVal) {
+ if (l == r) {
+ tree[idx] = newVal > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ if (pos <= mid)
+ update(idx * 2, l, mid, pos, newVal);
+ else
+ update(idx * 2 + 1, mid + 1, r, pos, newVal);
+ tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+ int query() {
+ return tree[1];
+ }
+ };
+
+ vector longestCommonPrefix(vector& words, int k) {
+ int n = words.size();
+ vector ans(n, 0);
+ if (n - 1 < k) return ans;
+ vector trie(1);
+ for (const string& word : words) {
+ int cur = 0;
+ for (char c : word) {
+ int idx = c - 'a';
+ if (trie[cur].children[idx] == 0) {
+ trie[cur].children[idx] = trie.size();
+ trie.push_back({0, trie[cur].depth + 1});
+ }
+ cur = trie[cur].children[idx];
+ trie[cur].count++;
+ }
+ }
+ int maxDepth = 0;
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie[i].count >= k) {
+ maxDepth = max(maxDepth, trie[i].depth);
+ }
+ }
+ vector globalCount(maxDepth + 1, 0);
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie[i].count >= k && trie[i].depth <= maxDepth) {
+ globalCount[trie[i].depth]++;
+ }
+ }
+ vector> fragileList(n);
+ for (int i = 0; i < n; ++i) {
+ int cur = 0;
+ for (char c : words[i]) {
+ int idx = c - 'a';
+ cur = trie[cur].children[idx];
+ if (trie[cur].count == k) {
+ fragileList[i].push_back(trie[cur].depth);
+ }
+ }
+ }
+ int segSize = maxDepth;
+ if (segSize >= 1) {
+ SegmentTree segTree(segSize, globalCount);
+ for (int i = 0; i < n; ++i) {
+ if (n - 1 < k) {
+ ans[i] = 0;
+ } else {
+ for (int d : fragileList[i]) {
+ segTree.update(1, 1, segSize, d, globalCount[d] - 1);
+ }
+ int res = segTree.query();
+ ans[i] = res == -1 ? 0 : res;
+ for (int d : fragileList[i]) {
+ segTree.update(1, 1, segSize, d, globalCount[d]);
+ }
+ }
+ }
+ }
+ return ans;
+ }
+};
\ No newline at end of file
diff --git a/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/Solution.java b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/Solution.java
new file mode 100644
index 0000000000000..6b158681eca70
--- /dev/null
+++ b/solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/Solution.java
@@ -0,0 +1,130 @@
+class Solution {
+ static class TrieNode {
+ int count = 0;
+ int depth = 0;
+ int[] children = new int[26];
+
+ TrieNode() {
+ for (int i = 0; i < 26; ++i) children[i] = -1;
+ }
+ }
+
+ static class SegmentTree {
+ int n;
+ int[] tree;
+ int[] globalCount;
+
+ SegmentTree(int n, int[] globalCount) {
+ this.n = n;
+ this.globalCount = globalCount;
+ this.tree = new int[4 * (n + 1)];
+ for (int i = 0; i < tree.length; i++) tree[i] = -1;
+ build(1, 1, n);
+ }
+
+ void build(int idx, int l, int r) {
+ if (l == r) {
+ tree[idx] = globalCount[l] > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ build(idx * 2, l, mid);
+ build(idx * 2 + 1, mid + 1, r);
+ tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+
+ void update(int idx, int l, int r, int pos, int newVal) {
+ if (l == r) {
+ tree[idx] = newVal > 0 ? l : -1;
+ return;
+ }
+ int mid = (l + r) / 2;
+ if (pos <= mid) {
+ update(idx * 2, l, mid, pos, newVal);
+ } else {
+ update(idx * 2 + 1, mid + 1, r, pos, newVal);
+ }
+ tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
+ }
+
+ int query() {
+ return tree[1];
+ }
+ }
+
+ public int[] longestCommonPrefix(String[] words, int k) {
+ int n = words.length;
+ int[] ans = new int[n];
+ if (n - 1 < k) return ans;
+
+ ArrayList trie = new ArrayList<>();
+ trie.add(new TrieNode());
+
+ for (String word : words) {
+ int cur = 0;
+ for (char c : word.toCharArray()) {
+ int idx = c - 'a';
+ if (trie.get(cur).children[idx] == -1) {
+ trie.get(cur).children[idx] = trie.size();
+ TrieNode node = new TrieNode();
+ node.depth = trie.get(cur).depth + 1;
+ trie.add(node);
+ }
+ cur = trie.get(cur).children[idx];
+ trie.get(cur).count++;
+ }
+ }
+
+ int maxDepth = 0;
+ for (int i = 1; i < trie.size(); ++i) {
+ if (trie.get(i).count >= k) {
+ maxDepth = Math.max(maxDepth, trie.get(i).depth);
+ }
+ }
+
+ int[] globalCount = new int[maxDepth + 1];
+ for (int i = 1; i < trie.size(); ++i) {
+ TrieNode node = trie.get(i);
+ if (node.count >= k && node.depth <= maxDepth) {
+ globalCount[node.depth]++;
+ }
+ }
+
+ List> fragileList = new ArrayList<>();
+ for (int i = 0; i < n; ++i) {
+ fragileList.add(new ArrayList<>());
+ }
+
+ for (int i = 0; i < n; ++i) {
+ int cur = 0;
+ for (char c : words[i].toCharArray()) {
+ int idx = c - 'a';
+ cur = trie.get(cur).children[idx];
+ if (trie.get(cur).count == k) {
+ fragileList.get(i).add(trie.get(cur).depth);
+ }
+ }
+ }
+
+ int segSize = maxDepth;
+ if (segSize >= 1) {
+ SegmentTree segTree = new SegmentTree(segSize, globalCount);
+ for (int i = 0; i < n; ++i) {
+ if (n - 1 < k) {
+ ans[i] = 0;
+ } else {
+ for (int d : fragileList.get(i)) {
+ segTree.update(1, 1, segSize, d, globalCount[d] - 1);
+ }
+ int res = segTree.query();
+ ans[i] = res == -1 ? 0 : res;
+ for (int d : fragileList.get(i)) {
+ segTree.update(1, 1, segSize, d, globalCount[d]);
+ }
+ }
+ }
+ }
+
+ return ans;
+ }
+}
\ No newline at end of file