Skip to content

Commit 3811f0b

Browse files
feat: add solution to lc problem: No.3485 (#4340)
1 parent 6837834 commit 3811f0b

File tree

4 files changed

+711
-4
lines changed

4 files changed

+711
-4
lines changed

Diff for: solution/3400-3499/3485.Longest Common Prefix of K Strings After Removal/README.md

+237-2
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,248 @@ tags:
114114
#### Java
115115

116116
```java
117-
117+
class Solution {
118+
static class TrieNode {
119+
int count = 0;
120+
int depth = 0;
121+
int[] children = new int[26];
122+
123+
TrieNode() {
124+
for (int i = 0; i < 26; ++i) children[i] = -1;
125+
}
126+
}
127+
128+
static class SegmentTree {
129+
int n;
130+
int[] tree;
131+
int[] globalCount;
132+
133+
SegmentTree(int n, int[] globalCount) {
134+
this.n = n;
135+
this.globalCount = globalCount;
136+
this.tree = new int[4 * (n + 1)];
137+
for (int i = 0; i < tree.length; i++) tree[i] = -1;
138+
build(1, 1, n);
139+
}
140+
141+
void build(int idx, int l, int r) {
142+
if (l == r) {
143+
tree[idx] = globalCount[l] > 0 ? l : -1;
144+
return;
145+
}
146+
int mid = (l + r) / 2;
147+
build(idx * 2, l, mid);
148+
build(idx * 2 + 1, mid + 1, r);
149+
tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
150+
}
151+
152+
void update(int idx, int l, int r, int pos, int newVal) {
153+
if (l == r) {
154+
tree[idx] = newVal > 0 ? l : -1;
155+
return;
156+
}
157+
int mid = (l + r) / 2;
158+
if (pos <= mid) {
159+
update(idx * 2, l, mid, pos, newVal);
160+
} else {
161+
update(idx * 2 + 1, mid + 1, r, pos, newVal);
162+
}
163+
tree[idx] = Math.max(tree[idx * 2], tree[idx * 2 + 1]);
164+
}
165+
166+
int query() {
167+
return tree[1];
168+
}
169+
}
170+
171+
public int[] longestCommonPrefix(String[] words, int k) {
172+
int n = words.length;
173+
int[] ans = new int[n];
174+
if (n - 1 < k) return ans;
175+
176+
ArrayList<TrieNode> trie = new ArrayList<>();
177+
trie.add(new TrieNode());
178+
179+
for (String word : words) {
180+
int cur = 0;
181+
for (char c : word.toCharArray()) {
182+
int idx = c - 'a';
183+
if (trie.get(cur).children[idx] == -1) {
184+
trie.get(cur).children[idx] = trie.size();
185+
TrieNode node = new TrieNode();
186+
node.depth = trie.get(cur).depth + 1;
187+
trie.add(node);
188+
}
189+
cur = trie.get(cur).children[idx];
190+
trie.get(cur).count++;
191+
}
192+
}
193+
194+
int maxDepth = 0;
195+
for (int i = 1; i < trie.size(); ++i) {
196+
if (trie.get(i).count >= k) {
197+
maxDepth = Math.max(maxDepth, trie.get(i).depth);
198+
}
199+
}
200+
201+
int[] globalCount = new int[maxDepth + 1];
202+
for (int i = 1; i < trie.size(); ++i) {
203+
TrieNode node = trie.get(i);
204+
if (node.count >= k && node.depth <= maxDepth) {
205+
globalCount[node.depth]++;
206+
}
207+
}
208+
209+
List<List<Integer>> fragileList = new ArrayList<>();
210+
for (int i = 0; i < n; ++i) {
211+
fragileList.add(new ArrayList<>());
212+
}
213+
214+
for (int i = 0; i < n; ++i) {
215+
int cur = 0;
216+
for (char c : words[i].toCharArray()) {
217+
int idx = c - 'a';
218+
cur = trie.get(cur).children[idx];
219+
if (trie.get(cur).count == k) {
220+
fragileList.get(i).add(trie.get(cur).depth);
221+
}
222+
}
223+
}
224+
225+
int segSize = maxDepth;
226+
if (segSize >= 1) {
227+
SegmentTree segTree = new SegmentTree(segSize, globalCount);
228+
for (int i = 0; i < n; ++i) {
229+
if (n - 1 < k) {
230+
ans[i] = 0;
231+
} else {
232+
for (int d : fragileList.get(i)) {
233+
segTree.update(1, 1, segSize, d, globalCount[d] - 1);
234+
}
235+
int res = segTree.query();
236+
ans[i] = res == -1 ? 0 : res;
237+
for (int d : fragileList.get(i)) {
238+
segTree.update(1, 1, segSize, d, globalCount[d]);
239+
}
240+
}
241+
}
242+
}
243+
244+
return ans;
245+
}
246+
}
118247
```
119248

120249
#### C++
121250

122251
```cpp
123-
252+
class Solution {
253+
public:
254+
struct TrieNode {
255+
int count = 0;
256+
int depth = 0;
257+
int children[26] = {0};
258+
};
259+
260+
class SegmentTree {
261+
public:
262+
int n;
263+
vector<int> tree;
264+
vector<int>& globalCount;
265+
SegmentTree(int n, vector<int>& globalCount)
266+
: n(n)
267+
, globalCount(globalCount) {
268+
tree.assign(4 * (n + 1), -1);
269+
build(1, 1, n);
270+
}
271+
void build(int idx, int l, int r) {
272+
if (l == r) {
273+
tree[idx] = globalCount[l] > 0 ? l : -1;
274+
return;
275+
}
276+
int mid = (l + r) / 2;
277+
build(idx * 2, l, mid);
278+
build(idx * 2 + 1, mid + 1, r);
279+
tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
280+
}
281+
void update(int idx, int l, int r, int pos, int newVal) {
282+
if (l == r) {
283+
tree[idx] = newVal > 0 ? l : -1;
284+
return;
285+
}
286+
int mid = (l + r) / 2;
287+
if (pos <= mid)
288+
update(idx * 2, l, mid, pos, newVal);
289+
else
290+
update(idx * 2 + 1, mid + 1, r, pos, newVal);
291+
tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]);
292+
}
293+
int query() {
294+
return tree[1];
295+
}
296+
};
297+
298+
vector<int> longestCommonPrefix(vector<string>& words, int k) {
299+
int n = words.size();
300+
vector<int> ans(n, 0);
301+
if (n - 1 < k) return ans;
302+
vector<TrieNode> trie(1);
303+
for (const string& word : words) {
304+
int cur = 0;
305+
for (char c : word) {
306+
int idx = c - 'a';
307+
if (trie[cur].children[idx] == 0) {
308+
trie[cur].children[idx] = trie.size();
309+
trie.push_back({0, trie[cur].depth + 1});
310+
}
311+
cur = trie[cur].children[idx];
312+
trie[cur].count++;
313+
}
314+
}
315+
int maxDepth = 0;
316+
for (int i = 1; i < trie.size(); ++i) {
317+
if (trie[i].count >= k) {
318+
maxDepth = max(maxDepth, trie[i].depth);
319+
}
320+
}
321+
vector<int> globalCount(maxDepth + 1, 0);
322+
for (int i = 1; i < trie.size(); ++i) {
323+
if (trie[i].count >= k && trie[i].depth <= maxDepth) {
324+
globalCount[trie[i].depth]++;
325+
}
326+
}
327+
vector<vector<int>> fragileList(n);
328+
for (int i = 0; i < n; ++i) {
329+
int cur = 0;
330+
for (char c : words[i]) {
331+
int idx = c - 'a';
332+
cur = trie[cur].children[idx];
333+
if (trie[cur].count == k) {
334+
fragileList[i].push_back(trie[cur].depth);
335+
}
336+
}
337+
}
338+
int segSize = maxDepth;
339+
if (segSize >= 1) {
340+
SegmentTree segTree(segSize, globalCount);
341+
for (int i = 0; i < n; ++i) {
342+
if (n - 1 < k) {
343+
ans[i] = 0;
344+
} else {
345+
for (int d : fragileList[i]) {
346+
segTree.update(1, 1, segSize, d, globalCount[d] - 1);
347+
}
348+
int res = segTree.query();
349+
ans[i] = res == -1 ? 0 : res;
350+
for (int d : fragileList[i]) {
351+
segTree.update(1, 1, segSize, d, globalCount[d]);
352+
}
353+
}
354+
}
355+
}
356+
return ans;
357+
}
358+
};
124359
```
125360
126361
#### Go

0 commit comments

Comments
 (0)